Results 1 to 19 of 19

Thread: Object Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    7

    Object Arrays

    sup everyone? I'm new here but I was just wondering how to create object arrays in VB.NET. Maybe I'm just blind but I cant seem to find any index property with any of the objects like it was in VB6. This is frustrating me as I have projects to complete and plan but I cannot seem to reslove this problem. Any help would be greatly appreciated. Also, I'm only in Grade 11 so please explain in simplest terms

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You can make object arrays just like you would any variable array. You can also use the arraylist to hold objects.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    7
    yup, I know about declaring an array variable as the type object... but in VB6, when designing a form you can use the index property to make an array of objects. I cant seem to do that in VB.NET

  4. #4
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    Thats because you have to do it the way that hellswraith said.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    7
    Ok, sorry to be a dummy, but I'm a beginner soooo, I declare the array variable as an object? for example:

    Dim labelarray(4) As Object


    And then how do I assign label objects to the array?

    labelarray(0) = Label1



    I'm beggin for your help! lol

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is one way to do it. First just lay out your labels or whatever controls you want in the array like norma. Then either at the form level or whenever you need to access them in an array do something like this:

    VB Code:
    1. Dim ctrls() as Control={Label1,Label2,Label3,Label4} 'put the names of whatever controls you want in the array
    2. 'then you can use it like a control array
    3. ctrls(2).Text="I'm the second in the array!"

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    7
    ooooooooooh! I see now! thankyou sooooo much. I really found it inconvenient that they took away the index property with objects. However, I'll have to live with it. So thanks again for the help, I really appreciate it.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    One really cool thing about this way is that you can change what is in the array at runtime. Also the array can contain different kinds of controls too. So if you want a few labels, a textbox, and a couple buttons in a control array then you can add them all to the same array.

    VB Code:
    1. Dim ctrls() as Control={Label1,Label2,TextBox1,Button1} 'put the names of whatever controls you want in the array
    2. 'then you can use it like a control array
    3. ctrls(2).Text="I'm the second in the array!"

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    7
    I guess that is quite usefull also... however the inconvenience is assigning the objects their respective spots in the array. it would be better to use a For Next loop but it is not possible because using the variable X to distinguish between objects does not assign the object to the array, but instead gives the array a invalid value.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Also each container control has a controls collection. This can be useful but not if the controls are on different containers. For instance if the labels are all on the form DIRECTLY then they will be in the Me.Controls collection, but if they are on a panel or a tab on the form then they will be in that controls Control collection.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    7
    I'm not very familiar with using the control collection. What is it used for?

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Its used by the containers to contain other controls.

  13. #13
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Is there any way to 'duplicate' a control in VB.NET (at runtime), as you could do in vb6 using ReDim (control arrays)?

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What do you mean by duplicate? Or redim a control array in VB6?

    In vb6 you used to use Load to add another control to a control array at runtime, is that what you mean?

    If so then in .NEt you just make a new control:
    dim lbl as New Label()

    You'll need to add it to a controls collection if you want it to show up on something:
    Me.Controls.Add(lbl)

  15. #15
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    hmm i have no idea why i said Redim...i meant load. oops.

    Ah alright i get it although how would you use a dynamic name for the new label (lbl01,lbl02,etc respectivly) since you cant have two controls with the same name?

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    7
    hmmm yup, thats what I forgot too, creating/using a dynamic name... hopefully someone can help us out here

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why do you need a dynamic name? Since its dynamic you can't refer to it else where anyway. Whether you load it into an Array or Collection you don't even need to name it. Or if you use a collection you can use teh key as the name.


    _Col.Add(New Label(),"Label" & _Col.Count)

  18. #18
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Ah, I didnt relize that you could load the new label in the .add statement like that. Alright that looks like what i needed, Thanks.

    Now I can start porting some of my VB6 apps over to .net, as they all relied completely on control arrays

  19. #19
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Well you can dynamically name them if you wish. It helps if you want to assign a common event handler for a click event to them all for example.
    I've posted my article on this before but here goes again:-

    http://www.developerfusion.com/show/2499/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width