Results 1 to 5 of 5

Thread: How do I add to an array?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    Chicago, Illinois
    Posts
    9

    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.
    I am the Code King

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The CopyTo method of the items collection provides an easy way to move the items to an array.

    VB Code:
    1. 'we take one away since count is one based and an array is zero based
    2.         Dim items(ListBox1.Items.Count - 1) As String
    3.         'I didn't use any error handling so watch out if the count is zero you will get an exception
    4.         ListBox1.Items.CopyTo(items, 0)
    5.         'the items array is now a copy of the items in the listbox

  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Neat, that's a much cleaner way than what I posted. Thanks Ed.

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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
  •  



Click Here to Expand Forum to Full Width