dynamic arrays

B

Brad

Help says I can create a dynamic array by doing this: Dim xxx_array() as
string
and then ReDim it before using it. But then it says you can save its
contents by doing this: ReDim Preserve xxx_array(50),
implying you can use it before the ReDim. So I tried this, but as soon as I
try to put something in the array, I get an out-of-bounds error which
appears to be caused by the empty parens being interpreted as dimension = 0.
I'm apparently missing some info on how this is done. Thanks for any help.

Brad
 
C

Chirag

'ReDim Preserve' would preserve data if its there. For instance, you have a
dynamic array (of zero length to start with), then you 'ReDim' it to some
length, fill it in with some data. If you now want to have more space in the
array, then you would use 'ReDim Preserve' on the array.

As a side note, if you are using dynamic arrays, you might want to look at
the Collection class as an alternative to dynamic arrays. Collections
automatically resize when required.

- Chirag

PowerShow - View multiple shows simultaneously
http://officerone.tripod.com/powershow/powershow.html
 
T

Tushar Mehta

No, the fact that Preserve is an option for ReDim doesn't mean that an
array can be used before it is dimensioned. What it means is that if
you *change* the dimension specification (of the last dimension only),
it is possible at that time to have the data preserved. Something like
the untested:

Dim X
ReDim X(1 to 10)
X(1)="1"
ReDim Preserve X(1 to 2)
MsgBox X(1)

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top