ReDim
The ReDim statement allows you to re-dimension the
size of a dynamic array at runtime. Why would you want to
do this? Imagine the case where you are populating an array
from a database but the number of items is always changing.
In most cases, you can just use a new array when you need an
array of a different size. Other times, it is more convenient
to keep the array name the same and thus resize the existing
array. This is done as shown below:
Dim aArray aArray = Array(10) ' now we have an array that holds 10 elements
ReDim aArray(20) ' now we have an array that holds 20 elements
The ReDim statement is often used with the Preserve
statement to resize an array without losing the exiting
information. An example of how this is done is shown below:
Dim aArray aArray = Array(0) nCount
Randomize nCount = Int(Rnd() * 10) + 1 For I = 1 To nCount ReDim Preserve aArray(I) aArray(I) = I Next
|