The array mentioned in this article is the definition of arrays in Visual Basic, which has certain reference value for Asp programming. Interested friends can refer to it.
An array is a collection of ordered data. Elements in an array may not belong to the same data type. Use a unified array name and subscript to uniquely determine the elements in the array. Changing one of the elements will not affect the other elements. The subscripts of an array are bounded and are divided into lower bounds and upper bounds. Arrays can be declared with Dim, Private, Public, or Static, and their syntax formats are the same. The following is only the method of declaring arrays using Dim.
1. Definition and declaration of arrays
The definition syntax of an array is as follows:
Dim array name ([[subscript lower bound To] subscript upper bound]) [As data type]
For example (assuming that the default lower bound of the array in the current module is 0)):
① Dim A(10) As Integer
It means that the array name is A. The lower bound of this array subscript is the default value 0, the upper bound of the subscript is 10, and there are 11 Integer-type elements, from A(0), A(1) to A(10).
② Dim B(1 To 20) As Integer
It means that the array name is B. The lower bound of this array is 1, the upper bound of the subscript is 20, and there are 20 elements of Integer type, from B(1) to B(20).
③Dim DayArray(50)
Indicates that DayArray is a Variant array with 51 indexes (from 0 to 50) elements.
④Dim Matrix(3, 4) As Integer
Indicates that Matrix is a two-dimensional Integer array.
⑤Dim MyMatrix(1 To 5, 4 To 9, 3 To 5) As Double
Indicates that MyMatrix is a three-dimensional double array that explicitly specifies the upper and lower bounds.
⑥Dim BirthDay(1 To 10) As Date
Indicates that BirthDay is a Date-type array with indexes from 1 to 10.
2. Option Base statement
The Option Base statement is used at the module level and is used to declare the default lower bound of array subscripts.
The syntax of the Option Base statement is as follows:
Option Base {0 | 1}
Note: The lower bound of the array is 0 by default, and there is no need to use the Option Base statement. If you use this statement to specify the array lower bound 1, you must use the Option Base statement before the module's array declaration.
Notice:
(1) The To clauses in Dim, Private, Public, ReDim and Static statements provide a more flexible way to control the subscript of an array. However, if the lower bound is not explicitly specified using the To clause, you can set the default lower bound to 1 using Option Base. The lower bounds of an array created using the Array function are also determined by the lower bounds specified by the Option Base statement, unless Array is qualified by the type library (such as VBA.Array) name, if qualified by the type library name, the array created using the Array function is also determined by the name of the Array function. The lower realm is not affected by Option Base.
(2) The Option Base statement only affects the lower bounds of the array located in the module containing the statement.
Some notes about array declaration:
①The array name naming rules are the same as variable names.
② The array name is wrapped in parentheses, and square brackets cannot be used, which is different from C language.
③The lower bound of the subscript must not be greater than its upper bound.
④ You can use variable names or constant names (and real numbers) as the boundary for subscripts. When the boundary of the index is a constant name, the size of the array is fixed. When the boundary of the index is a variable name, the size of the array can be dynamically defined, that is, the size of the array depends on the value of the variable during the program operation. Therefore, arrays in VB can be divided into two types: static arrays and dynamic arrays.
3. Static array
A static array means that the number of array elements is fixed and unchanged, that is, the size of memory space they occupy is fixed and unchanged. Depending on the different dimensions of a fixed-size array, it can be divided into one-dimensional arrays and multi-dimensional arrays.
The syntax format for declaring multidimensional arrays is:
Dim array name ([subscript boundary list]) [As data type]
The definition form of subscript boundary: [subscript lower boundary To] subscript upper boundary
The subscript boundary list refers to the subscript boundary of each dimension of an array separated by a comma, that is,
[Subscript lower bound To] subscript upper bound, [Subscript lower bound To] subscript upper bound,..., [Subscript lower bound To] subscript upper bound
(First dimension) (Second dimension) (Nth dimension)
When n=1, the array is called a one-dimensional array; when n=2, the array is called a two-dimensional array; and so on, when n=m, the array is called an m-dimensional array.
The following examples are used to introduce the use of one-dimensional arrays.
'Declare a string array of length 51 FriendsName
Dim FriendsName(50) As String
'Declare a global integer array of length 11
Public Class(10) As Integer
The number of elements in a one-dimensional array is (upper boundary-lower boundary+1).
To assign initial values to an array, loop statements can be used, such as:
Dim I As Integer
For I = 0 To 11 'Control of program flow usage of loop statements
C(I) = I
Next I
If the subscript lower bound is not explicitly specified, the subscript lower bound of the array is controlled by the Option Base statement. If there is no Option Base statement, the lower bound is default to 0.
The dimensions of an array are not limited to 2. In VB, it can be expanded to 60. In practical applications, there are not many applications for arrays above three-dimensionality. When defining a multi-dimensional array, you only need a Dim statement to specify all subscript boundaries of the array. Using multi-dimensional arrays can easily represent some meaningful statistical data. For example:
Dim Profit(16,10,12) As Currency
This Profit array can be used to represent the profits of a department store with the store name, department and month as parameters. For example: Profit (2,8,11) represents the profit of the eighth department of the second branch in November.
4. Dynamic array
Sometimes, before the array starts to be used, it is impossible to know how large the array is needed to meet the actual needs. Of course, the size of the array can be defined large enough to meet any practical application needs. This method is very inefficient (a lot of waste of memory space). If you use dynamic arrays, you can accurately define the size of the array according to actual needs when the program is running.
When declaring an array, not giving a list of dimensions is to declare the array as a dynamic array. For example:
Dim MyArry() As Integer
Before using dynamic arrays, it must be redefined using the ReDim statement. As the array MyArry declared earlier, you can use the following statement to define it as a dynamic two-dimensional array.
ReDim MyArry(10,10)
You can also define dynamic arrays multiple times by repeatedly executing ReDim statements. The maximum dimension of an array that can be defined using ReDim is 60. The ReDim statement can change the number of elements per dimension of an array, but cannot change the number of dimensions. The following is an example of a standard application of the ReDim statement.
Dim MyArry() As Single 'Declares Dynamic Array
ReDim MyArry(30,20,10) 'Redefine array
ReDim MyArry(50,23,21) 'Redefine the array again
ReDim's syntax is the same as Dim's, and it also has the choice of Preserve keyword:
ReDim Preserve array name ([[[subscript lower bound To]subscript upper bound]) [As data type]
For example:
?
- ReDimMyArry(50,23,21)
- ReDimPreserveMyArry(50,23,50)