Dynamic Array means that the subscript in the brackets is not given when the array size is declared.) When you want to use it, use the ReDim statement to re-specify the array size at any time. The advantage is that the storage space can be effectively used according to user needs. The dynamic array is defined as follows:
Dim array name () [As data type] 'declares an array with no defined size or its data type.
An undefined array cannot be used. You can use the ReDim statement to redefine the array size. The format is as follows:
ReDim [Preserve] array name (expression)
You can use ReDim to redefine the array size multiple times. However, if the ReDim statement does not include the Preserve keyword, the data stored before the array is cleared after the array size is redefined. If you want to retain the previous data, use the Preserve keyword.
For example:
Dim arr () As Integer 'defines the dynamic array arr Dim n As Integer = 10 ReDim arr (n) 'redefines the array arr and defines its size as 11 For I = 0 To 10 Arr (I) = I Next ReDim Preserve arr (20) 're-defines the array arr, defines its size as 21, and retains 'Arr (0 )~ Value of arr (10) |
Note: The subscript in the static array declaration can only be a constant, while the subscript in the dynamic array ReDim statement can be a constant or a variable with a fixed value. For example, the preceding arr (n ).
You can use ReDim multiple times to change the array size.
BibliographyPrevious sectionNext section |