VB Dynamic Arrays: Define & Assign Values

In VB, dynamic arrays can be defined and assigned values using the ReDim statement. Here is an example:

Dim myArray() As Integer

ReDim myArray(4) '定义一个包含5个元素的整型数组

'给数组赋值
myArray(0) = 1
myArray(1) = 2
myArray(2) = 3
myArray(3) = 4
myArray(4) = 5

In the given example, a dynamic array called myArray is first defined using the Dim statement, then the ReDim statement is used to specify the size of the array as 5. Finally, the elements of the array are assigned values using indexes.

It is important to note that when using the ReDim statement to redefine the size of an array, the data in the original array will be lost. If you need to preserve the data in the original array, you can use the Preserve keyword as shown below:

ReDim Preserve myArray(9) '重新定义数组大小为10,并保留原数组中的数据
bannerAds