Results 1 to 5 of 5

Thread: Looping thru an array, but ReDim in the loop?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350

    Looping thru an array, but ReDim in the loop?

    Hi Gang,

    I'm looping through an array, using UBound as the upper limit of the loop, thus:

    VB Code:
    1. For myCount = 0 to UBound(myArray) - 1
    2. '  do some stuff
    3. Next

    No prob so far.

    Then I realised I needed to grown the array under certain circs, inside the loop thus:

    VB Code:
    1. For myCount = 0 to UBound(myArray) - 1
    2. '  do some stuff
    3. '
    4. Redim Preserve myArray(somevalue)
    5. '
    6. Next

    Now the loop doesn't continue to the new end of the array; it still stops at the original end. Even tho' in step mode, if I park the mouse over the For stmt, which shows the UBound has increased, VB seems to have an internal representation of the For..Next's outer limit which it doesn't update.

    I've worked around in a way I think is a bit ugly... set the loop's outer limit to be bigger than I expect and then check if I go past the end of the array, thus:

    VB Code:
    1. For myCount = 0 to 50      '50 > largest expected value
    2. If myCount > UBound(myArray) Then Exit For
    3. '
    4. '  do some stuff
    5. '
    6. Redim Preserve myArray(somevalue)
    7. '
    8. Next

    Any one got any better ideas please?
    .

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    could you maybe have a counter that counts each time you add to the array.. then when you are all done you redim to that value?

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Don't use a FOR loop for that, it should only be used when you know the definite bounds to loop BEFORE it starts, try a DO loop instead:

    VB Code:
    1. myCount = 0
    2. Do While myCount < UBound(myArray)
    3.   '  do some stuff
    4.   '
    5.   Redim Preserve myArray(somevalue)
    6.   '
    7.   myCount = myCount + 1
    8. Loop

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350
    Thanks Si I'll try that...

    Cheers.
    .

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    I would actually suggest that if u sorta know that the array may grow u should set the new size before you enter the loop and then reset it back if necessary after the loop completes. Redim preserving inside a loop is extremely slow.

    eg Pseudo

    Original array is 20 elements

    I'm gonna add 5 elements in the loop maybe
    Redim Preserve Array (25)
    Looping looping
    Add a few items
    End loop
    Hey I only added 3
    Redim Preserve (23)
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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