【英文】VB的数组

Preface

Notes on VB arrays

One-dimensional Arrays

Declaring Static Arrays

Defining the number of arrays

Define one array at a time
1
Dim ArrayName(...)
Define multiple arrays at once
1
Dim ArrayName(...), ArrayName(...)

Specifying the data type

Not specifying the data type
  • If the data type is not specified, it defaults to variant.
1
Dim ArrayName(...)
Directly specifying the data type
1
Dim ArrayName(...) As DataType
Specifying the data type with type identifiers
1
Dim ArrayNameDataTypeIdentifier(...)

Specifying the array length

Specifying the starting and ending indexes
1
Dim ArrayName(MinimumIndex To MaximumIndex)
Only specifying the ending index
  • If the starting index is not specified, it defaults to 0.
1
Dim ArrayName(MaximumIndex)
Uniformly specifying the minimum value for all intervals
  • The starting value of the index specified by the Option Base keyword applies to each dimension of the array if multiple-dimensional arrays are defined.
1
2
Option Base MinimumIndex
Dim ArrayName(MaximumIndex)

Declaring Dynamic Arrays

  • When declaring dynamic arrays using Dim, no storage space is allocated and the array cannot be referenced.
  • The dimension and size of the array can only be determined after the array is redeclared using ReDim, at which point storage space is allocated.
    • The array can be redeclared multiple times to change its length and dimensions, but the data type of the array cannot be changed.

Specifying the data type

Not specifying the data type
1
2
3
4
5
6
7
8
9
' Define dynamic array
Dim ArrayName()

' Define/change the length of the array
ReDim ArrayName(MinimumIndex To MaximumIndex)
ReDim ArrayName(MaximumIndex)

' Define/change the dimensions of the array
ReDim ArrayName(Definition of the Dimension-1, Definition of the Dimension-2, ...)
Directly specifying the data type
1
2
3
4
5
6
7
8
9
' Define dynamic array
Dim ArrayName() As DataType

' Define/change the length of the array
ReDim ArrayName(MinimumIndex To MaximumIndex) As DataType
ReDim ArrayName(MaximumIndex) As DataType

' Define/change the dimensions of the array
ReDim ArrayName(Definition of the Dimension-1, Definition of the Dimension-2, ...) As DataType
Specifying the data type with type identifiers
1
2
3
4
5
6
7
8
9
' Define dynamic array
Dim ArrayNameDataTypeIdentifier()

' Define/change the length of the array
ReDim ArrayNameDataTypeIdentifier(MinimumIndex To MaximumIndex)
ReDim ArrayNameDataTypeIdentifier(MaximumIndex)

' Define/change the dimensions of the array
ReDim ArrayNameDataTypeIdentifier(Definition of the Dimension-1, Definition of the Dimension-2, ...)

Saving data

  • After the array is redeclared using ReDim, all data is cleared. To prevent data from being cleared, the keyword Preserve can be used.
1
2
3
4
5
6
' Define dynamic array
Dim ArrayName()

' Define/change the length of the array
ReDim ArrayName(MinimumIndex To MaximumIndex)
ReDim ArrayName(MaximumIndex)

Modifying dimensions

  • When redeclaring an array using ReDim, if the original array is multidimensional, only the minimum index of the last dimension can be modified and the dimensions of the array cannot be changed.
1
2
3
4
5
' Define dynamic array
Dim ArrayName(Definition of the Dimension-1, Definition of the Dimension-2)

' Define/change the dimensions of the array
ReDim ArrayName(Definition of the Dimension-1, Definition of the Dimension-2)

Assigning Values to Array Elements

Assigning a value to a specific index

1
ArrayName(Index) = Value

Assigning values to all elements using the Array function

  • The Array() function can be used to assign values to an array during the compilation phase, saving runtime, and improving program execution efficiency.
1
ArrayName = Array(Value1, Value2, ...)
Assigning values to dynamic arrays
1
ArrayName() = Array(Value1, Value2, ...)

Assigning values to all elements by splitting a string

1
ArrayName = Split(String, Delimiter)

Retrieving the Value of Array Elements

1
ArrayName(Index)

Multidimensional Arrays

Declaring Arrays

  • In an array, multiple ranges of indices are defined, each range representing a dimension. Multiple ranges of indices are separated by commas.
  • For specific methods of defining indices, refer to Specifying the array length.
  • For specific methods of defining data types, refer to Specifying the data type.
1
Dim ArrayName(Definition of the Dimension-1, Definition of the Dimension-2, ...)

Assigning Values to Array Elements

1
ArrayName(Dimension-1 Index, Dimension-2 Index, ...) = Value

Retrieving the Value of Array Elements

1
ArrayName(Dimension-1 Index, Dimension-2 Index, ...)

Getting the Minimum and Maximum Index of an Array

Getting the minimum index of an array

Dimension: Starts from 1

1
2
3
LBound(ArrayName)

LBound(ArrayName, Dimension)

Getting the maximum index of an array

1
2
3
UBound(ArrayName)

UBound(ArrayName, Dimension)

Traversing Arrays

Using the For Loop

1
2
3
For i = LBound(ArrayName) To UBound(ArrayName)
...
Next i

Using the For…Each Loop

  • The For…Each loop starts from the array index of 0.
1
2
3
For Each i In ArrayName
...
Next i

Control Arrays

  • All elements in a control array must be controls of the same type.
  • The controls in a control array have the same control name.
  • The controls in a control array can have different property values other than the name property.
  • The elements in a control array are distinguished by the Index property of the control, which ranges from 0 to 32767.
  • The indexes of the elements in a control array can be discontinuous.
  • All controls in a control array share the same event, and the event procedure name of a control array is followed by a pair of parentheses with an Index parameter inside to determine which control triggered the event.

Creating Control Arrays

Method 1

  • By copying and pasting controls

When creating the first control, its Index property value is empty.
When pasting a control:

  • The Index property value of the control before pasting is 0.
  • The Index property value of the pasted control is 1.
  • The Index of subsequent pasted controls is incremented by 1.

Method 2

  • When creating the second control of the same type, specify the same control name as the first control.

Method 3

  • When creating a control, modify the Index property.

Adding and Removing Elements in an Array

  • Adding an element to an existing control array

Before using the Load() method, create a control array in design mode.
Elements added using the Load() method are not visible by default. The Visible property is set to False by default and needs to be manually set to True to display the elements.
After adding an element, the Left and Top properties need to be set in order to modify the position of the element on the form.

1
Load ControlArrayName(Index)
  • Removing an element from an existing control array

Only controls added using the Load() method can be removed. Controls added in design mode cannot be removed.

1
UnLoad ControlArrayName(Index)

Conclusion

References

Bilibili - JiaoDaYou