|
-
Oct 30th, 2002, 08:05 PM
#1
Thread Starter
New Member
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
-
Oct 30th, 2002, 08:57 PM
#2
PowerPoster
You can make object arrays just like you would any variable array. You can also use the arraylist to hold objects.
-
Oct 30th, 2002, 09:01 PM
#3
Thread Starter
New Member
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
-
Oct 30th, 2002, 09:26 PM
#4
Addicted Member
Thats because you have to do it the way that hellswraith said.
-
Oct 31st, 2002, 07:19 PM
#5
Thread Starter
New Member
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
-
Oct 31st, 2002, 10:06 PM
#6
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:
Dim ctrls() as Control={Label1,Label2,Label3,Label4} 'put the names of whatever controls you want in the array
'then you can use it like a control array
ctrls(2).Text="I'm the second in the array!"
-
Nov 2nd, 2002, 11:21 AM
#7
Thread Starter
New Member
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.
-
Nov 2nd, 2002, 09:07 PM
#8
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:
Dim ctrls() as Control={Label1,Label2,TextBox1,Button1} 'put the names of whatever controls you want in the array
'then you can use it like a control array
ctrls(2).Text="I'm the second in the array!"
-
Nov 2nd, 2002, 11:18 PM
#9
Thread Starter
New Member
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.
-
Nov 3rd, 2002, 12:43 AM
#10
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.
-
Nov 3rd, 2002, 11:34 AM
#11
Thread Starter
New Member
I'm not very familiar with using the control collection. What is it used for?
-
Nov 3rd, 2002, 04:33 PM
#12
Its used by the containers to contain other controls.
-
Nov 3rd, 2002, 05:00 PM
#13
Addicted Member
Is there any way to 'duplicate' a control in VB.NET (at runtime), as you could do in vb6 using ReDim (control arrays)?
-
Nov 3rd, 2002, 07:09 PM
#14
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)
-
Nov 3rd, 2002, 10:00 PM
#15
Addicted Member
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?
-
Nov 3rd, 2002, 11:21 PM
#16
Thread Starter
New Member
hmmm yup, thats what I forgot too, creating/using a dynamic name... hopefully someone can help us out here
-
Nov 3rd, 2002, 11:49 PM
#17
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)
-
Nov 4th, 2002, 06:13 PM
#18
Addicted Member
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
-
Nov 4th, 2002, 07:31 PM
#19
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|