Entry
What is a Dynamic Array and how is it used?
Jun 11th, 2000 13:10
Jerry Yoakum, Exam Cram: MS VB 5, Exam #70-165 - 157610236X
A dynamic array is an array that can be resized "dynamically" so that
it can grow or shrink as needed. To declare a dynamic array, omit the
number of elements in the Dim (or Public, Private, or Static) statement:
Dim iDynamic() as Integer
When you determine the number of elements required, you allocate the
storage with the ReDim keyword. You can repeatedly use ReDim to alter
the number of elements in an array, and the subscript may be specified
as a literal, a variable, or the result of a function.
ReDim iDynamic(12) as Integer
ReDim iDynamic(iCount) as Integer
ReDim iDynamic(Sq(36)) as Integer
Redimensioning the array destroys all of the data contained in the
array unless you use the Preserve keyword. If you increase the number
of elements in an array and use Preserve, all of the data in the array
is preserved and the new elements are initialized. If you shrink the
size of the array, only that data in the remaining elements is
retained. When you used the Preserve keyword, only the upper bound of
the last dimension can be modified. You can, however, change the number
of dimensions if you are not using Preserve.
NOTE:
Technically, you do not need to specify the data type of an array
when redimensioning the array; it retains its original data type.
However, it is good practice to do so.
NOTE:
Use Erase to clear the contents of an array. If the array is fixed,
all values are initialized. If the array is dynamic, all memory is
reclaimed. To use the dynamic array again, you will have to ReDim it.