VB Script Variables
We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.
We declare multiple variables by separating each variable name with a comma. For
Dim x, y, city, gcreddyWe 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.
Option Explicit Statement
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.
Variable names follow the standard rules for naming anything in VBScript. A variable name:
a) Must begin with an alphabetic character.
b) Cannot contain an embedded period.
c) Must not exceed 255 characters.
d) Must be unique in the scope in which it is declared.
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.
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.
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”
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.
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) = 55Similarly, the data can be retrieved from any element using an index into the particular array element you want.
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.
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.
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 Preserve MyArray(30)
There is no limit to the number of times we can re size a dynamic array, although if we make an array smaller, we lose the data in the elimination