Results 1 to 2 of 2

Thread: [RESOLVED] Dynamic arrays

  1. #1

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Resolved [RESOLVED] Dynamic arrays

    I have an array that I'm not sure of what size it will be. How should I go about declaring it? I know I can declare it as dynamic and then ReDim it, but I won't know what size to make it until data is retrieved and added to the array. Should I just declare it's size to be much larger that it will be? If so, how can I tell the size of the array (meaning what part of the array actually has data)?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Dynamic arrays

    Generally, in cases like this, one declares it as dynamic as you have mentioned.

    Remember that you can resize the array and keep the contents by using the keyword Preserve...
    Code:
    Dim myArray() As Long
    ....
      When data is received, increase the array enough to include the new data
    ReDim Preserve myArray(0 To [some new size])
       When more data is received, you should know how much is being received
    ReDim Preserve myArray(0 To [some new size])
        etc, etc
    Now there are more efficient ways of doing this. But maybe a bit more detail might help. This should get you started.

    Oversizing the array is a good technique most of the time. To determine how big it is, use UBound(myArray). When all the data has been received and you need to have the array exactly that size, the use ReDim Preserve one last time to resize it to fit.

    P.S. There is a topic on arrays in the FAQs section
    Last edited by LaVolpe; Feb 8th, 2009 at 09:08 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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