Home>

An array is a collection of ordered data.The elements in the array may not belong to the same data type.Use a uniform array name and subscript to uniquely identify the elements in the array,Changing one element does not affect the other elements.The index of the array is bounded,Divided into lower and upper bounds.Arrays can be declared with dim, private, public, or static. They have the same syntax.The following only introduces the method of declaring arrays with dim.

1. Definition and declaration of arraysThe definition of an array is as follows:

dim array name ([[subscript lower bound to] subscript upper bound]) [as data type]

For example (assuming the default lower bound of the array is 0 in the current module):

① dim a (10) as integer

Indicates that the array name is a, the subscript lower bound of this array is the default value 0, the upper bound of the subscript is 10, and there are 11 elements of type integer.From a (0), a (1) to a (10).

② dim b (1 to 20) as integer

Indicates that the array name is b, the subscript lower bound of this array is 1, the upper bound of the subscript is 20, and there are 20 elements of type integer.From b (1) to b (20).

③dim dayarray (50)

Indicates that dayarray is a variant array with 51 indices (from 0 to 50).

④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 with explicitly specified upper and lower bounds.

⑥dim birthday (1 to 10) as date

Indicates that birthday is a date array with indices from 1 to 10.

2. option base statement

The option base statement is used at the module level,Used to declare the default lower bound of the array index.

The syntax of the option base statement is as follows:

option base {0 | 1}

Note:By default, the lower bound of the array is 0. At this time, you do not need to use the option base statement. If you use this statement to specify an array lower bound of 1, you must use the option base statement before the module's array declaration.

note:

(1) The to clauses in dim, private, public, redim, and static statements provide a more flexible way to control the indexing of arrays.However, if the lower bound is not explicitly specified using the to clause,You can use option base to set the default lower bound to 1. The lower bound of an array created using the array function is also determined by the lower bound specified by the option base statement. Unless array is qualified by a type library (e.g. vba.array) name,If qualified by type library name,The lower bound of an array created using the array function is not affected by option base.

(2) The option base statement only affects the lower bound of the array located in the module containing the statement.

A few notes about array declarations:

① The rules for naming array names are the same as for variable names.

② The name of the array is enclosed in parentheses.You cannot use square brackets,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 of subscripts.When the boundary of a subscript is a constant name,The size of the array is fixed,When the boundary of a subscript is a variable name,The size of the array can be defined dynamically,That is, the size of the array depends on the value of the variable during the running of the program.Therefore, arrays in vb can be divided into two types of arrays:static arrays and dynamic arrays.

3. Static array

The number of elements in a static array is constant.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 arrays and multi-dimensional arrays.

The syntax for declaring a multidimensional array is:

dim array name ([subscript boundary list]) [as data type]

Subscript boundary definition form:[subscript lower bound to] subscript upper bound

The subscript boundary list refers to the subscript boundaries of the dimensions of the array separated by commas,which 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 m-dimensional array.

The following example introduces the use of one-dimensional arrays.

"Declares a string array friendsname of length 51

dim friendsname (50) as string

"Declaring a global integer array class of length 11

public class (10) as integer

The number of elements in a one-dimensional array is (upper bound-lower bound + 1).

You can use loop statements to assign initial values ​​to arrays.Such as:

dim i as integer

for i=0 to 11 "Control of program flow using 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 defaults to 0.

The number of dimensions of the array is not limited to 2. In vb, it can be expanded to 60. In practical applications, there are not many applications of arrays that are more than three-dimensional.When defining a multi-dimensional array, only one dim statement is required to specify all the subscript boundaries of the array.Using multi-dimensional arrays can easily represent some meaningful statistical data.E.g:

dim profit (16,10,12) as currency

This profit array can be used to represent the profit 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 is used,I don't know how big the array is to meet the actual needs.Of course, the size of the array can be defined to be large enough to meet any practical application needs.This method is very inefficient (a lot of wasted memory space). If using dynamic arrays,You can run the program according to actual needs,Define exactly the size of the array.

When declaring an array,Not giving a list of dimensions is declaring the array as a dynamic array.E.g:

dim myarry () as integer

Before using dynamic arrays,It must be redefined using the redim statement.As the array myarry declared earlier, you can define it as a dynamic two-dimensional array with the following statement.

redim myarry (10,10)

You can also define a dynamic array multiple times by repeatedly executing a redim statement.The maximum number of dimensions that can be defined using redim is 60. The redim statement can change the number of elements in each dimension of the array.But the dimension cannot be changed.The following is an example of a standard application of the redim statement.

dim myarry () as single "Declares a dynamic array

redim myarry (30,20,10) "Redefine the array

redim myarry (50,23,21) "Redefine the array again

The syntax of redim is the same as that of dim. In addition, it has the option reserve keywords:

redim preserve array name ([[subscript lower bound to] subscript upper bound]) [as data type]

E.g:

redim myarry (50,23,21)
redim preserve myarry (50,23,50)

note:When using redim, redefining the array will make all array element values ​​disappear,The use of preserve can retain data.But using preserve can only change the size of the last dimension of the array,Keep the data of the array.For a one-dimensional array,All data is retained,For multi-dimensional arrays:the size of the last dimension can only be changed,To keep all the array data,Otherwise, an error occurs.

5. lbound and ubound functions

Both the lbound function and the ubound function return a long data. The former value is the minimum index available for the specified array dimension.The latter gets the maximum index.Their syntax is:

lbound (array name [, specified dimension])

ubound (array name [, specified dimension])

The array name is required.The specified number of dimensions is optional,Indicates which dimension of the lower bound to return.1 represents the first dimension,2 represents the second dimension,And so on.If the specified number of dimensions is omitted,It defaults to 1.

For how to use lbound and ubound functions, see the following example:

dim a (1 to 100,3, -3 to 4) as integer "defines a three-dimensional array,It is assumed that the default value of the lower bound of the array is not changed using the option base statement.

Use lbound and Ubound functions for array a. The return value list is as follows

The default lower bound for all dimensions depends on the setting of the option base statement.It can be seen that using lbound and ubound functions on an array can be used to determine the number of elements in an array.

For arrays where the to clause is used in the declaration to set the number of dimensions,They can use any integer as a lower bound without being limited by the option base statement.

6. Advanced features of arraysAlthough arrays are most commonly used to store groups of variables,But arrays are also useful in other ways.You can assign the contents of one array to another, create a function that returns an array,You can also create attributes that return an array.in many circumstances,These technologies can improve application performance.

Just as you can assign the value of one variable to another,For example, stra=strb, you can also assign the contents of one array to another.For example, to copy a byte array from one location to another.This can be achieved by copying one byte at a time,The procedure is as follows:

sub bytecopy (oldcopy () as byte, newcopy () as byte)
"The parameter oldcopy () is the source array,newcopy () is the target array
dim i as integer
redim newcopy (lbound (oldcopy) to ubound (oldcopy)) "Redefined dynamic array
for i=lbound (oldcopy) to ubound (oldcopy) "cyclic assignment
newcopy (i)=oldcopy (i)
next
end sub

A more effective and simple way is to directly assign an array to another array:

sub bytecopy (oldcopy () as byte, newcopy () as byte)
"The parameter oldcopy () is the source array,newcopy () is the target array
newcopy=oldcopy "Assign directly using array
end sub

There are some rules to keep in mind about variable assignment.For example, while a variable declared as an integer can be assigned to a variable declared as a long without any problems,But assigning a long variable to an integer variable can easily cause an overflow error.In addition to following the rules regarding assignment between data type variables,Array assignment also follows other rules,Including array dimensions,The size of each dimension,And whether the array is fixed or dynamic.

①Several factors to consider for array assignments with different dimensions and data types

1. The type of the array to the left of the assignment:fixed array (dim x (1 to 10) as integer) or dynamic array (dim x () as integer).

2. Whether the dimension of the array on the left of the assignment symbol matches the dimension of the array on the right of the assignment symbol.

3. Whether the number of array elements in each dimension of the array on both sides of the assignment symbol matches.Even if the array declarations are different,The dimensions may also match.For example, each dimension of an array is numbered starting from 0 and the other is starting from 1. The number of dimensions 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 variable assignment.

Array assignment errors may occur at compile time,It can also happen at runtime (for example, if the data type cannot be coerced or the assignment attempts to redefine a static array). Design your program to add error handling to ensure that the arrays match before assignment.

②Write a function that returns an array

It is possible to return a set of values ​​from a function.For example, returning a set of bytes from a function without having to convert it to a string first and then back again.

Here is a simple example using a function that returns a byte array:

private sub form_load ()
dim b as byte
dim i as integer
dim returnarray () as byte
b=cbyte (54)
returnarray ()=arrayfunction (b) "Call function
for i=lbound (returnarray) to ubound (returnarray)
msgbox returnarray (i) "loop display array values ​​through popup message box
next
end sub
public function arrayfunction (b as byte) as byte ()
dim x (2) as byte
x (0)=b
x (1)=b + cbyte (200)
x (2)=b + b
arrayfunction=x "returns the result as an array x
end function

After running the above example, returnarray () is a three-element array,It 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 data type of the function (byte in this case). Because this is a function call,You do not need to pass parentheses when passing an array.

note:

1. Although it is possible to return an array by assigning it to another array (arrayfunction=x ()),But for performance reasons,This method is not recommended.

2. You must specify a type for a function that returns an array.This type can be a variant. Thus,function x () as variant () is valid and function x () as () will fail.

3. When calling a function that returns an array,The variable used to hold 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 will be displayed.

asp
  • Previous Detailed explanation of ajax request json data
  • Next C # Resources