VB Script Variables

A variable is a convenient placeholder that refers to a computer memory location where we can store program information that may change during the time our script is running.

3.1 Declaring Variables

We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.

For example:

Dim city
Dim x

We declare multiple variables by separating each variable name with a comma. For


Dim x, Top, Bottom, Left, Right

We can also declare a variable implicitly by simply using its name in our script. That is not generally a good practice because we could misspell the variable name in one or more places, causing unexpected results when our script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables.

The Option Explicit statement should be the first statement in our script.

3.2 Option Explicit

Forces explicit declaration of all variables in a script.

Option Explicit ' Force explicit variable declaration.
Dim MyVar ' Declare variable.
MyInt = 10 ' Undeclared variable generates error.
MyVar = 10 ' Declared variable does not generate error.

3.3 Naming Restrictions for Variables

Variable names follow the standard rules for naming anything in VBScript. A variable name:
o Must begin with an alphabetic character.
o Cannot contain an embedded period.
o Must not exceed 255 characters.
o Must be unique in the scope in which it is declared.

3.4 Scope of Variables

A variable's scope is determined by where we declare it.

When we declare a variable within a procedure, only code within that procedure can access or change the value of that variable.

If we declare a variable outside a procedure, we make it recognizable to all the procedures in our script. This is a script-level variable, and it has script-level scope.

3.5 Life Time of Variables

The lifetime of a variable depends on how long it exists.

The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running.

At procedure level, a variable exists only as long as you are in the procedure.

3.6 Assigning Values to Variables

Values are assigned to variables creating an expression as follows:

The variable is on the left side of the expression and the value you want to assign to the variable is on the right.

For example:
A = 200
City = “Hyderabad”

X=100: Y=200

3.7 Scalar Variables and Array Variables

A variable containing a single value is a scalar variable.

A variable containing a series of values, is called an array variable.

Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name.

Dim A(3)

Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements.

We assign data to each of the elements of the array using an index into the array.
Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows:

A(0) = 256
A(1) = 324
A(2) = 100
A(3) = 55

Similarly, the data can be retrieved from any element using an index into the particular array element you want.

For example:

SomeVariable = A(4)

Arrays aren't limited to a single dimension. We can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions.

In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:

Dim MyTable(5, 10)

In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.

3.8 Dynamic Arrays

We can also declare an array whose size changes during the time our script is running. This is called a dynamic array.

The array is initially declared within a procedure using either the Dim statement or using the ReDim statement.

However, for a dynamic array, no size or number of dimensions is placed inside the parentheses.

For example:
Dim MyArray()
ReDim AnotherArray()

To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.

In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

ReDim MyArray(25)

ReDim Preserve MyArray(30)

There is no limit to the number of times we can resize a dynamic array, although if we make an array smaller, we lose the data in the eliminated elements.

Post a Comment


Close Menu