Data Type: Any
The data to be assigned to the first array element.
Data Type: Any
Any number of data items you wish to add to the array.
A variant array consisting of the arguments passed into the function.
Returns a variant array containing the elements whose values are passed to the function as arguments.
The code fragment:
vaMyArray = Array("Mr", "Mrs", "Miss", "Ms")
is similar to writing:
vaMyArray(0) = "Mr"
vaMyArray(1) = "Mrs"
vaMyArray(2) = "Miss"
vaMyArray(3) = "Ms"
creates a variant array, you can pass any data type, including objects, to the
function. You can also pass the values returned by calls to other
functions to create multidimensional arrays; these kinds of arrays are called "ragged" arrays.
Rules at a Glance
Although the array you create with the
function is a variant array data type, the individual elements of the array can be a mixture of different data types.
The initial size of the array you create is the number of arguments you place in the argument list and pass to the
The lower bound of the array created by the
function is 0.
The array returned by the
function is a dynamic rather than a static array. Once created, you can redimension the array using
, or another call to the
If you don't pass any arguments to the
function, an empty array is created. Although this may appear to be the same as declaring an array in the conventional manner with the statement:
Dim myArray( )
the difference is that you can then use the empty array with the
function again later in your code.
myArray = Array(100, 2202, 3.3, 605, 512)
Programming Tips and Gotchas
function was not present in the first version of VBScript and was added to the language in Version 2.
You cannot assign the return value of
to a variable previously declared as an array variable. Therefore,
declare the variant variable as an array using the normal syntax:
Dim myArray( )
Instead, simply declare a variant variable, such as:
function is ideal for saving space and time and for writing more efficient code when creating a fixed array of known elements, for example:
Title = Array("Mr", "Mrs", "Miss", "Ms")
You can use the
function to create multidimensional arrays. However, accessing the elements of the array needs a little more thought. The following code fragment creates a simple two-dimensional array with three elements in the first dimension and four elements in the second:
vaListOne = Array(Array(1, 2, 3, 4), _
Array(5, 6, 7, 8), _
Array(9, 10, 11, 12))
Surprisingly, the code you'd expect to use to access the array returns a "Subscript out of range" error:
'This line generates a Subscript out of range error
Response.Write vaListOne(1, 2)
Instead, since this is an array stored within an array (that is, a ragged array), you can access it as
Because you declare the variant variable to hold the array as a simple variant, rather than an array and can then make repeated calls to
, the function can create dynamic arrays. For example, the following code fragment dimensions a variant to hold the array, calls
to create a variant array, then calls
again to replace the original variant array with a larger variant array:
varArray = Array(10,20,30,40,50)
varArray = Array(10,20,30,40,50,60)
of using this method is that while it makes it easy to replace an array with a different array, it doesn't allow you to easily expand or contract an existing array.
Unlike Visual Basic, VBScript does not contain an
statement; therefore, arrays created in VBScript using the
function have a lower boundary of 0. That is, the first element of the array will always be accessed using an index value of 0.
Dim Statement, LBound Function, ReDim Statement, UCase Function