An array is a collection of ordered data. Elements in an array may not belong to the same data type. Using a uniform array name and subscript to uniquely determine an element in an array, changing one of the elements does not affect other elements. The subscript of an array is bounded, and is divided into lower and upper bounds. Arrays can be declared with dim, Private, public, or static, and their syntax is in the same format. The following is only a way to declare an array with dim.
1. The definition and declaration of an array
the definition syntax for the array is as follows:
Dim array name ([[[Lower Subscript to] subscript upper bound]) [as data type]
For example (assuming that the missing bounds of an array in the current module is 0)):
①dim A (A) as Integer
Represents an array named a, the lower bound of this array is the default value of 0, the subscript upper bound is 10, and 11 elements of the integer type, from a (0), a (1) to a (10).
②dim B (1 to) as Integer
Represents an array named B, the lower bound of this array is 1, the upper bound is 20, and 20 elements of the integer type, from B (1) to B (20).
③dim Dayarray (50)
Indicates that the Dayarray is a Variant array with 51 indexes (from 0 to 50) elements.
④dim Matrix (3, 4) as Integer
Indicates that the 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) as Date
Indicates that the birthday is a date array of index 1 through 10.
2. Option Base Statement
The Option Base statement is used at the module level to declare the default lower bound of the array subscript.
The syntax for the Option Base statement is as follows:
Option Base {0 | 1}
Note: The lower bound of the array is 0 by default, and you do not need to use the Option Base statement at this time. If you use this statement to limit the lower 1 of the array, you must use the option Base statement before the array declaration of the module.
Attention:
(1) The TO clause in Dim, Private, Public, ReDim, and Static statements provides a more flexible way to control the subscript of an array. However, if you do not explicitly specify a lower bound using the TO clause, you can use Option Base to set the default lower bound to 1. The lower bound of an array created with the array function is also determined by the lower bound specified by the Option Base statement, unless the array is by a type library (for example, VBA). Array) name qualification, if it is qualified by the type library name, the lower bound of an array created with the array function is not affected by Option Base.
(2) The Option Base statement affects only the lower bounds of the array in the module that contains the statement.
A few notes on the array declaration:
① array name rules are the same as variable names.
② array names are enclosed in parentheses, not square brackets, unlike C.
The lower bound of the ③ subscript must not be greater than its upper bound.
④ can be labeled with variable names or constant names (and real numbers). When the boundary of the subscript is a constant, the size of the array is fixed, and the boundary of the current object is the 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's operation. Therefore, the array in VB can be divided into two kinds of arrays: static array, dynamic array.
3. Static array
A static array is the number of exponential group elements that are fixed, that is, the amount of memory space they occupy is fixed. Depending on the number of dimensions of a fixed-size array, it can be divided into one-dimensional and multidimensional 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 bound to] subscript upper bound
The subscript boundary list refers to the subscript boundary of each dimension of an array separated by commas, i.e.
[Subscript lower to] subscript upper bound, [subscript lower 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, and when n=2, the array is called a two-dimensional array, and so on, and so on, when n=m, the array is called an M-dimensional array.
The following examples describe the use of one-dimensional arrays.
' Declare a string array of length 51 friendsname
Dim Friendsname as String
' Declares a global integer array of length 11 class
Public Class (a) as Integer
The number of elements in a one-dimensional array is (upper bound-lower bound + 1).
Assigning an initial value to an array can take a circular statement, such as:
Dim I as Integer
Control of the use program flow for I = 0 to 11 ' circular statements
C (i) = I
Next I
If you do not explicitly specify a subscript lower bound, the lower bound of the array is controlled by the Option Base statement. If there is no Option Base statement, the lower bound is the default of 0.
The number of dimensions of the array is not limited to 2, in VB, can be extended to 60, in the actual application of three-dimensional above the application of the array is not much. When you define a multidimensional array, just a Dim statement specifies all the subscript boundaries of the array, and using multidimensional arrays can be a convenient way to represent some meaningful statistical data. For example:
Dim Profit (16,10,12) as Currency
This profit array can be used to represent the profit of a department store with a store name, department, and month as parameters. For example: Profit (2,8,11) represents the second branch of the division in November profits.
4. Dynamic array
Sometimes before an array can be used, it is not possible to know how large an array is needed to meet actual needs. Of course, the size of the array can be defined enough to meet any practical application needs, this method is very inefficient (a large amount of waste of memory space). If you use a dynamic array, you can precisely define the size of the array when the program is running, depending on your actual needs.
When you declare an array, you do not give the dimension list of columns to declare the array as a dynamic array. For example:
Dim Myarry () as Integer
Before using a dynamic array, you must redefine it using the ReDim statement. As previously declared array Myarry, you can define it as a dynamic two-dimensional array using the following statement.
ReDim Myarry (10,10)
You can also define a dynamic array multiple times by repeatedly executing the ReDim statement. The maximum number of dimensions that can define an array using ReDim is 60. The ReDim statement can change the number of elements per dimension of an array, but it cannot change the number of dimensions. The following is an example of a standard application for ReDim statements.
Dim Myarry () as single declares a 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, and it has a choice preserve keyword:
ReDim Preserve array name ([[[Subscript lower to] subscript upper bound]) [as data type]
For example:
ReDim Myarry (50,23,21)
ReDim Preserve Myarry (50,23,50)
Note: When you use ReDim, redefining the array causes the values of all the array elements to disappear and use preserve to preserve the data. But using preserve can only change the size of the last dimension of the array, preserving the data of the array. For a one-dimensional array, all data is preserved, and for multidimensional arrays: Only the last dimension can be changed to preserve all of the array data, otherwise there will be an error.
5. LBound functions and UBound functions
Both the LBound function and the UBound function return a Long data whose value is the lowest subscript available for the specified array dimension, and the latter gets the maximum subscript. Their syntax is:
LBound (array name [, specified number of dimensions])
UBound (array name [, specified number of dimensions])
Where the array name is required. The specified number of dimensions is optional, indicating which dimension's lower bound is returned. 1 means the first dimension, 2 represents the second dimension, and so on. If the specified number of dimensions is omitted, it is assumed to be 1.
See the following example for how to use the LBound function with the UBound function:
Dim A (1 to 100,3,-3 to 4) as Integer defines a 13-d array, assuming that the default value of the lower bounds of the array is not changed using the option Base statement.
Array a uses the LBound and UBound functions, and the return value list is as follows
The default lower bound for all dimensions depends on the setting of the Option Base statement. You can see that using the LBound function and the UBound function for an array can be used to determine the number of elements in an array.
For arrays that use the to clause in a declaration to set a dimension, they can use any integer as the lower bound without being limited by the option Base statement.
6. Advanced Features of arrays
Although arrays are most commonly used to store groups of variables, arrays are also useful in other ways. You can assign the contents of an array to another array, create a function that returns an array, and create properties that return an array. In many cases, these technologies can improve the performance of your application.
Just as you can assign the value of a variable to another variable, such as Stra = STRB, you can also assign the contents of an array to another array. For example, to copy a byte-type array from one location to another. You can do this by copying one byte at a time, and the program is as follows:
The Sub bytecopy (Oldcopy () as Byte, Newcopy () as Byte)
' parameter oldcopy () is the source array, and newcopy () is the target array
Dim I as Integer
ReDim Newcopy (Lbound (oldcopy) to UBound (oldcopy)) ' redefine dynamic array for
i = Lbound (oldcopy) to UBound (oldcopy) ' Loop assignment
newcopy ( i) = Oldcopy (i)
Next end
Sub
A more efficient and simple method is to assign an array directly to another array:
Sub bytecopy (Oldcopy () as Byte, Newcopy () as Byte)
' parameter oldcopy () is the source array, newcopy () is the destination array
newcopy = Oldcopy ' Using arrays to directly assign an end
Sub
There are some rules to remember about variable assignment. For example, although you can assign a variable declared as an integer to a variable that is declared to be a long integer without any problems, assigning a long integer variable to an integer variable could easily result in an overflow error. In addition to complying with the rules for assigning values between data type variables, array assignments follow other rules, including array dimensions, the size of each dimension, and whether the array is fixed or dynamic.
Several factors considering the ① dimension and different data type array assignment
1, the array type to the left of the assignment: a fixed array (Dim x (1 to) as Integer) or a dynamic array (Dim x () As Integer).
2. Whether the dimension of the array to the left of the assignment matches the dimension of the array to the right of the assignment.
3. The number of array elements in each dimension of the array on both sides of the assignment matches. A dimension may match even if the declaration of an array is different. For example, each dimension element of an array is numbered from 0 and the other begins with 1, and the dimension may also match.
4. The data types of all elements on both sides of the assignment must be compatible. These rules are consistent with the rules for assigning values to variables.
An error in an array assignment can occur at compile time or at run time (for example, if the data type cannot be cast or an assignment attempts to ReDim a static array). You want to add error handling when you design your program to make sure that the array matches before assigning values.
② to write a function that returns an array
It is possible to return a set of values from a function. For example, you can return a set of bytes from a function without having to convert first to a string and then back again.
The following is a simple example of a function that uses a return byte array:
Private Sub Form_Load ()
Dim B As Byte
Dim i As Integer
Dim ReturnArray () As byte
B = CByte ()
Returna Rray () = ArrayFunction (b) ' Call function for
i = Lbound (ReturnArray) to Ubound (ReturnArray)
Msgbox returnarray (i) ' via pop-up message Box loop display array value
Next end
Sub public
Function arrayfunction (b as Byte) as Byte ()
Dim x (2) as Byte
x (0) = b< C13/>x (1) = B + CByte ($)
x (2) = B + b
arrayfunction = X ' Returns the array x end
Function
After running the example above, ReturnArray () is a group of ternary primes that contains the values assigned to the array in arrayfunction. The ArrayFunction statement passes an array as a parameter, and the data type of the array must be the same as the function's data type (in this case, bytes). Because this is a function call, it is not necessary to pass the array with parentheses.
Attention:
1. Although you can return an array by assigning a value to another array (arrayfunction = x ()), this method is not recommended for performance reasons.
2, you must specify a type for the function that returns the array, this type can be a Variant. As a result, the function X () as Variant () is valid and function X () as () fails.
3. When you call a function that returns an array, the variable that holds the return value must also be an array, and its data type must be the same as the function return type, otherwise a "type mismatch" error is displayed.
The above is a small series and you share the ASP definition array method, interested in the small partners can refer to the