|
-
May 1st, 2003, 11:17 PM
#1
Thread Starter
New Member
How do I add to an array?
Greets Folks,
I'm having probs and need help.
example mArray () = {"sam","smith"} ' This is a literal declaration which is straightforward and easy to manipulate.
I'm looking to do something like this:
mArray(0) = intSomeInt ' This isn't working for me
What's the proper way to get a variable into an array element?
I'm trying to take the items from a List Box on a Form and store them into an array. The array will be Private and declared in a Class Module. How do I get the items from the List Box and store them in the array elements in the Class Module?
Put List Box Item1 into mArray(0)
Put List Box Item2 into mArray(1)..........and on down the list
If you can help me, I'll be very grateful, Thanks.
-
May 2nd, 2003, 02:12 AM
#2
Fanatic Member
I'm looking to do something like this:
mArray(0) = intSomeInt ' This isn't working for me
Not sure why it doesn't work for you, it works for me and I can't really think of another way people might be using...
I'm trying to take the items from a List Box on a Form and store them into an array. The array will be Private and declared in a Class Module. How do I get the items from the List Box and store them in the array elements in the Class Module?
Put List Box Item1 into mArray(0)
Try something like:
Code:
'resize mArray appropriate to the length of listboxitems
'if keeping contents are important use redim preserve
ReDim mArray(ListBox1.Items.Count)
Dim lbiFoo As String
Dim iCount As Integer = 0
For Each lbiFoo In ListBox1.Items
mArray(iCount) = lbiFoo
iCount += 1
Next
-
May 2nd, 2003, 03:20 AM
#3
The CopyTo method of the items collection provides an easy way to move the items to an array.
VB Code:
'we take one away since count is one based and an array is zero based
Dim items(ListBox1.Items.Count - 1) As String
'I didn't use any error handling so watch out if the count is zero you will get an exception
ListBox1.Items.CopyTo(items, 0)
'the items array is now a copy of the items in the listbox
-
May 2nd, 2003, 03:44 AM
#4
Fanatic Member
Neat, that's a much cleaner way than what I posted. Thanks Ed.
-
May 2nd, 2003, 04:49 AM
#5
Fanatic Member
All arrays and collections expose a CopyTo method? That's handy, I should have known that.
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
|