Variables are used for storing values, like text strings, numbers or arrays.
Variables in VBScript are declared as follows:
variable_name = value
The following rules apply when naming variables in VBScript:
Rule 1: A variable name must begin with an alphabetic character
Rule 2: A variable name cannot contain a period ( . )
Rule 3: A variable name cannot be longer than 255 characters
Rule 4: A variable name must be unique in the scope in which it is declared
Creating variables in VBScript is often referred to as "declaring" variables. VBScript variables are declared using the Dim, Public, or Private keywords:
Once a variable has been declared, it can be assigned a value:
myVariable = 10
In addition, variables can be declared and assigned a value at the same time:
Dim myVariable = 10
Note: Variables declared outside any function, and variables first used within functions without being declared using Dim, have a "global" scope.
An array variable is used to store multiple values in a single variable.
The number shown in the parentheses is 10. This indicates that the array contains 3 elements and is a fixed-size. Data can be assined to each of the elements of the array like this:
names(0) = "Jack" names(1) = "Elizabeth" names(2) = "Will"
Arrays can have more than one dimension. Multiple dimension arrays are declared by separating the numbers in the parentheses with commas.
This example creates a two-dimensional array consisting of 4 rows and 12 columns:
Assigning data to a multi-dimensional array is similar to the creation, separate the number with commas:
names(0,0) = "Jack" names(0,1) = "Elizabeth" names(0,2) = "Will" names(1,0) = "Dauntless" names(1,1) = "Interceptor" names(1,2) = "Black Pearl"