10 out of 21 rated this helpful - Rate this topic

Array Function

Updated: April 2009

Returns a Variant containing an array.


                      Array(
                      arglist
                      )
                    

The required arglist argument is a comma-delimited list of values that are assigned to the elements of an array contained with the Variant. If no arguments are specified, an array of zero length is created.

The notation used to refer to an element of an array consists of the variable name followed by parentheses containing an index number indicating the desired element.

All arrays in Visual Basic Scripting Edition (VBScript) are zero-based.

The Array function is used to create single-dimension arrays. For information about multi-dimensional arrays, see VBScript Variables.

Note Note:

A variable that is not declared as an array can still contain an array. Although a Variant variable containing an array is conceptually different from an array variable containing Variant elements, the array elements are accessed in the same way.

The following example illustrates the use of the Array function.

' Create a single-dimension array and assign three elements.
Dim A
A = Array(10,20,30)
' Set B to the third element in the array (which is 30).
' The array is zero-based.
B = A(2)

Date

History

Reason

April 2009

Added information to remarks and added comments to the example.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Some handy Array tools I've Built for VBScript
I totally agree that working with Arrays in VBScript is more than painful. It's excruciating. So... I'll share with you some functions I've written to help with working with them. They are maintained as I find bugs with them, but obviously I can't fix the language itself so there are probably some limitations on the use of these tools.

ArrayLength and ArrayAppend: https://gist.github.com/901874

Example Usage:
' ArrayAppend & ArrayLength usage:
Dim names()

ArrayLength(names) ' returns 0

names = ArrayAppend(names, "John")

ArrayLength(names) ' returns 1

names = ArrayAppend(names, "Jacob")
names = ArrayAppend(names, "Jingleheimer")
names = ArrayAppend(names, "Schmidt")

ArrayLength(names) ' returns 4

inArray: https://gist.github.com/943116

Example Usage:
' Assume we're still in the scope of above example.
inArray(names, "Jacob") ' returns 1 (index of Jacob in names())
inArray(names, "foobar") ' returns -1
inArray(names, "Schmidt") ' returns 3 (index of Schmidt in names())

' How to check if a value exists in the array:
If inArray(names, "John") > -1 Then
' do something
End If
How to declare and display array using VBscript
Syntax: Array(size)
If you want to declare an array with three elements then declare size as 2 and the syntax is as follows.

Dim arr(2)

inserting elements into an array
------------------------------------
 arr(0)="a"
arr(1)="b"
arr(2)="c"

Displaying array elements
-----------------------------
foreach ele in arr
   document.write(ele)
next
A bug in evaluation size of array
Here is the sample:

Dim size
size = 0

Dim arrList()
If IsArray(arrList) Then
    ' The following lines will throw out runtime error
    size = LBound(arrList)
    size = UBound(arrList)
End IF
  • 9/24/2010
  • JZW
I totally agree...
I couldn't agree more. Working with arrays in VBScript is really a royal pain in the underside of the digestive system, especially when you compare it to other languages.
Array Object Needs Redesigning!

There needs to be a better set of tools to work with the array object; if the Array is empty, functions such as UBound and LBound will only throw exception errors, and there is not a lot in the way of tests that can trap this error--and besides, LBound is a pointless functions since it ALWAYS returns 0, unless the array is actually empty, in which case it throws an exception.

isNull(array)


won't work because if the array has been defined, it won't be Null. Further, trying to determine the emptiness of an array is not exactly graceful, either, since its use in any formula throws an exception.

What's really needed is something along the order of a "size" function that returns how many elements are in the array, specifically 0 (zero) if there are no elements--without throwing up an exception error.

This has been an issue for as long as there have been vb arrays...It's really time to fix this shortcoming!