Used at procedure level to reallocate storage space for an array variable.
ReDim [ Preserve ] name(boundlist)
- Optional. Keyword used to preserve the data in the existing array when you change the size of only the last dimension.
- Required. Name of the variable. Must be a valid Visual Basic identifier. You can redimension as many variables as you like in the same statement, specifying the name and boundlist parts for each one. Multiple variables are separated by commas.
- Required. List of non-negative integers representing the upper bounds of the dimensions of the redefined array. Multiple upper bounds are separated by commas. The number of dimensions in boundlist must match the original rank of the array.
Each value in boundlist specifies the upper bound of a dimension, not the length. The lower bound is always zero, so the subscript for each dimension can vary from zero through the upper bound.
It is possible to use -1 to declare the upper bound of an array dimension. This signifies that the array is empty but not Nothing, a distinction required by certain common language runtime functions. However, Visual Basic code cannot successfully access such an array. If you attempt to do so, an IndexOutOfRangeException error occurs during execution.
The ReDim statement can appear only at procedure level. This means you can redefine arrays inside a procedure but not at class or module level.
The ReDim statement is used to change the size of one or more dimensions of an array that has already been formally declared. ReDim cannot change the rank (the number of dimensions) of the array.
The ReDim statement cannot change the data type of an array variable or provide new initialization values for the array elements.
ReDim releases the existing array and creates a new array with the same rank. The elements of the new array are initialized to the default value for their data type unless you specify Preserve.
If you include the Preserve keyword, Visual Basic copies the elements from the existing array to the new array. When you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same size it already has in the existing array.
For example, if your array has only one dimension, you can resize that dimension and still preserve the contents of the array, because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension if you use Preserve.
The following example increases the size of the last dimension of a dynamic array without losing any existing data in the array, and then decreases the size with partial data loss:
Dim IntArray(10, 10, 10) As Integer ' ... ReDim Preserve IntArray(10, 10, 20) ' ... ReDim Preserve IntArray(10, 10, 15)
The first ReDim creates a new array, copying all the elements from the existing array. It also adds 10 more columns to the end of every row in every layer. The elements in these new columns are initialized to the default value of the element type of the array.
The second ReDim creates another new array, copying all the elements that fit. However, five columns are lost from the end of every row in every layer. This is not a problem if you have finished using these columns. Reducing the size of a large array can free up memory that you no longer need.
You can use ReDim on a property that holds an array of values.
This example uses the ReDim statement to allocate and reallocate storage space for array variables.
Dim I, MyArray() As Integer ' Declare variable and array variable. ReDim MyArray(5) ' Allocate 6 elements. For I = 0 To UBound(MyArray) MyArray(I) = I ' Initialize array. Next I
The next statement resizes the array without saving the contents of the elements.
ReDim MyArray(10) ' Resize to 11 elements. For I = 0 To UBound(MyArray) MyArray(I) = I ' Initialize array. Next I
The following statement resizes the array but saves the contents of the elements.
ReDim Preserve MyArray(15) ' Resize to 16 elements.