How-to: VB Script Array Variables

An array variable can be created either locally with 'Dim', or as a public or private variable.

One dimensional Array

The maximum size of the array is determined by the subscript value in brackets:

DIM variableName(subscript)

Example:

Dim arrCoffee(2)

arrCoffee(0) = "Expresso"
arrCoffee(1) = "Latte"
arrCoffee(2) = "Cappuchino"

wscript.echo arrCoffee(2)
The lower bound of an array is always zero, so in the above example the subscript is 2 yet the array holds 3 items.

Two dimensional Array

A 2 dimensional array can be thought of as like a spreadsheet with X rows and Y columns, each holding one value.

The definition has 2 subscripts for X and Y:

DIM varname(SubscriptX, SubscriptY)

Example:

Dim arrCoffee(2,1)

arrCoffee(0,0) = "Expresso"
arrCoffee(0,1) = "No"
arrCoffee(1,0) = "Latte"
arrCoffee(1,1) = "Yes"
arrCoffee(2,0) = "Cappuchino"
arrCoffee(2,1) = "Yes"

wscript.echo "Coffee: " + arrCoffee(2,0) + " Milk: " + arrCoffee(2,1)

Multi dimensional Array

A multi dimensional array can be constructed just like a 2 dimensional one, adding extra subscripts for each extra dimension up to a maximum of 600.

DIM varname(SubscriptX, SubscriptY, SubscriptZ...)

“Working in an office with an array of electronic devices is like trying to get something done at home with half a dozen small children around.
The calls for attention are constant” ~ Marilyn vos Savant

Related VBScript commands

Variables - Define VBScript variables.
Set variable = object - Assign an object reference.


 
Copyright © 1999-2024 SS64.com
Some rights reserved