|
-
Nov 3rd, 2003, 07:51 PM
#1
Thread Starter
Fanatic Member
Collections... <RESOLVED>
Hiya doin vb ppl?
Ok, got a lot of questions here... I just moved from vb6 to vb.net... and was wondering about a lot of stuff!!!
K, first of all, in VB6 there were collections...
for instance, you could have 60 textbox with the same name
and do a quick call with formulas for the index... for example:
VB Code:
For tmpval = 1 To 20 Step 2
txtBox(tmpval).Text = "Number: " & Str(tmpval * 2)
Next tmpval
For tmpval = 1 To 20
If txtBox(tmpval).Text = "" Then txtBox(tmpval).Text = "Unknown number!"
Next tmpval
... you get the idea... now the collection (index property) IS NO MORE!!!!!!! How can I manipulate my objects with formulas now... ???
And if you have any tips on other problems I might encounter coz Im moving from vb6 to .net, please post em rigt away.... plus I noticed that vb.net had a lot of "friends" everywhere!!! like as friendly and stuff... uh, WHAT? lol, post me back ppl!
Oh yeah, I was also wondering how you could notice if an object is loaded...?
because my method, well, sucks:
I try to load an object, if an error occurs, and the error corresponds with the object bering allready loaded, than it resumes and continue another proces... but this method causes the program to do an error and isn't effective to my opinion, is there any other logical way to do it??? (I did that in vb6, now im lost with the index/collection disapearing and stuff...!)
Last edited by Ruku; Nov 10th, 2003 at 10:26 AM.
-
Nov 3rd, 2003, 10:19 PM
#2
The collections thing you mention are commonly referred to as Control Arrays. The lack of them in .NET is like the first stubbling block of all converters from VB6, but good news this question comes up all the time. Just do a quick search for 'Control Array' in the .NET section and you will get lots of helpful advise. The general bit is to use AddHandler or the Handles keyword to catch events for multiple controls in the same event handler.
Although to do the code you showed you'd just have to make a big ass array of the controls you want and then run the exact same code. Something like this:
VB Code:
'make control array
Dim txtBox() As TextBox={TextBox1,TextBox2,TextBox3,TextBox4,TextBox%,TextBoxYouGetThePicture}
For tmpval = 1 To 20 Step 2
txtBox(tmpval).Text = "Number: " & (tmpval * 2).ToString
Next
For tmpval = 1 To 20
If txtBox(tmpval).Text = "" Then txtBox(tmpval).Text = "Unknown number!"
Next
The bigger question should be why the heck do you need 60 textboxess.
I'm not sure what you mean by the object being 'loaded'. You can check if it is nothing just like in VB6.
-
Nov 5th, 2003, 04:59 PM
#3
Thread Starter
Fanatic Member
Originally posted by Edneeis
The collections thing you mention are commonly referred to as Control Arrays. The lack of them in .NET is like the first stubbling block of all converters from VB6, but good news this question comes up all the time. Just do a quick search for 'Control Array' in the .NET section and you will get lots of helpful advise. The general bit is to use AddHandler or the Handles keyword to catch events for multiple controls in the same event handler.
Although to do the code you showed you'd just have to make a big ass array of the controls you want and then run the exact same code. Something like this:
VB Code:
'make control array
Dim txtBox() As TextBox={TextBox1,TextBox2,TextBox3,TextBox4,TextBox%,TextBoxYouGetThePicture}
For tmpval = 1 To 20 Step 2
txtBox(tmpval).Text = "Number: " & (tmpval * 2).ToString
Next
For tmpval = 1 To 20
If txtBox(tmpval).Text = "" Then txtBox(tmpval).Text = "Unknown number!"
Next
The bigger question should be why the heck do you need 60 textboxess.
I'm not sure what you mean by the object being 'loaded'. You can check if it is nothing just like in VB6.
lol, the 60 textboxes was just an example.... and for loaded... I just mean to check if the object with the array allready exists... for instance, making a sub creating an object with a free array...
checking from 1 to 60... finding 2 doesn't exists, create txtBox(2)... get it? And im not sure if there was a way to do it in vb6...
And there is no quicker way to add objects in an array?!?!
VB Code:
Dim txtBox() As TextBox={TextBox1,TextBox2, ...]
So if I need 43243291823 objects I have to input them all one by one in the code????? There must be another way to load a bunch of objects... no?
-
Nov 5th, 2003, 05:08 PM
#4
Sure you can load the array via a loop if you want.
VB Code:
'load all textboxes that or on the form itself NOT another container
Dim al as New ArrayList
For Each txt As TextBox In Me.Controls
'if the textbox isn't already in the array then load it
If Not al.Contains(txt) Then al.Add(txt)
'of course in this example none will be since I just initialized the array
'before the loop
Next
'to return a type specific array from an arraylist use ToArray
Dim txt() As TextBox=al.ToArray(gettype(TextBox))
You can use the contains method or any ICollection object to see if something is already in it. The thing to keep in mind with this is that you will be checking specific instances for a match.
-
Nov 7th, 2003, 09:33 AM
#5
Thread Starter
Fanatic Member
Originally posted by Edneeis
Sure you can load the array via a loop if you want.
VB Code:
'load all textboxes that or on the form itself NOT another container
Dim al as New ArrayList
For Each txt As TextBox In Me.Controls
'if the textbox isn't already in the array then load it
If Not al.Contains(txt) Then al.Add(txt)
'of course in this example none will be since I just initialized the array
'before the loop
Next
'to return a type specific array from an arraylist use ToArray
Dim txt() As TextBox=al.ToArray(gettype(TextBox))
You can use the contains method or any ICollection object to see if something is already in it. The thing to keep in mind with this is that you will be checking specific instances for a match.
Ahhh, now were talkin! Thx, that really helps a lot of problems I had trouble with!!! But for the object loaded statment, what could I do?
-
Nov 7th, 2003, 10:46 AM
#6
If loaded and already in the array are the same thing then the Contains method takes care of it. Contains checks the array for an object. If loaded means intialized to an instance then test if it is Nothing.
-
Nov 10th, 2003, 10:25 AM
#7
Thread Starter
Fanatic Member
Alrit
Originally posted by Edneeis
If loaded and already in the array are the same thing then the Contains method takes care of it. Contains checks the array for an object. If loaded means intialized to an instance then test if it is Nothing.
Thanks, I guess that shuts me up... for the moment
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
|