Sometimes we cannot estimate the size of the array when using it, so it is easy to think of dynamic arrays, that is, dynamically changing the size of the array during use. This is easy to implement in VB. Let’s use a specific example to explain:
Defining a dynamic array in VB usually involves two steps:
(1) Use Dim, Private or Public to declare an array without subscripts in the form layer or standard module (the brackets cannot be omitted),
(2) Use the ReDim statement to define an array with subscripts in the process
The function of the ReDim statement is to re-point the size of the array. It allocates storage space when the program executes the ReDim statement.
Format: ReDim(array name)(subscript)[As(type)]
Function: Change the size and storage space of dynamic array.
illustrate:
(1) The subscript can be a constant or a variable with a definite value.
(2) The meaning of each parameter in the statement is the same as the statement using Dim to define an array.
(3) The ReDim statement can only be used for dynamic arrays. It can change the size of each dimension, but not the number of dimensions.
(4) When the program is compiled, all arrays in the ReDim statement are declared as dynamic arrays. When the program is running, when the ReDim statement is executed, the new upper and lower bounds are reassigned to the array, the values of the array elements will be initialized, the values of all numerical elements are set to 0, and the string elements are set to empty characters. string.
(5) The ReDim statement can define an array in the same way as the Dim statement. In the same program, the ReDim statement can also be used multiple times. Before using the :ReDim statement to redefine the array, you can use the :Erase statement to delete the original array.
Example:
Dimaa()AsLong' defines a dynamic array
Redimaa(10)'Adjust the size of dynamic arrays
dim i as integer
for i=1 to 10
aa(i) = i
next
Redim aa(5) 'Resize again
...Other operations