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?