unexplicable "subscript out of range" (VB5)

  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

I'm doing some coding in VB5 and my program is doing something really strange:
Code: [ Select ]
ReDim Preserve picFiles(1 To 1, 1 To 3) 'this works ok

      'add a new item to array and insert listed item
ReDim Preserve picFiles(LBound(picFiles, 1) To (UBound(picFiles, 1) + 1), 1 To 3) 'this gives subscript out of range, why?
  1. ReDim Preserve picFiles(1 To 1, 1 To 3) 'this works ok
  2.       'add a new item to array and insert listed item
  3. ReDim Preserve picFiles(LBound(picFiles, 1) To (UBound(picFiles, 1) + 1), 1 To 3) 'this gives subscript out of range, why?


Note that this is VB5 and not VBScript, but I would assume there wouldn't be much difference

Any Ideas?
  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

Straight from MSDN:

Quote:
Only the upper bound of the last dimension in a multidimensional array can be changed when you use the Preserve keyword; if you change any of the other dimensions, or the lower bound of the last dimension, a run-time error occurs. Thus, you can use code like this:

ReDim Preserve Matrix(10, UBound(Matrix, 2) + 1)

But you cannot use this code:

ReDim Preserve Matrix(UBound(Matrix, 1) + 1, 10)


If you only want to shift one of the dimensions, put it as the last one.
Otherwise, you should think about using a collection (I love them).

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 52 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.