A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running. For example, you might create a variable called ClickCount to store the number of times a user clicks an object on a particular Web page. Where the variable is stored in computer memory is unimportant. What is important is that you only have to refer to a variable by name to see or change its value. In VBScript, variables are always of one fundamental data type, Variant.
You can start using a variable even without declaring it in VBScript. Its type will be determined by the value you are storing in it.
Example: varX = 10
Here the type of varX is Integer.
varY = 12.50
Here the type of varY is Double.
varZ = “San Francisco”
Here the type of varZ is String.
varB = True
Here the type of varB is Boolean.
The way of using a variable shown above is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your 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 your script.
The Declaring of a variable using Option Explicit is shown below:
You declare variables explicitly in your script using the Dim statement, the Public statement, and the Private statement. For example:
You declare multiple variables by separating each variable name with a comma. For example:
Dim Top, Bottom, Left, Right
Variable names follow the standard rules for naming anything in VBScript. A variable name:·
- Must begin with an alphabetic character.·
- Cannot contain an embedded period.·
- Must not exceed 255 characters.·
- Must be unique in the scope in which it is declared.
Scope and Lifetime of Variables
A variable’s scope is determined by where you declare it. When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is a procedure-level variable. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your 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. When the procedure exits, the variable is destroyed. Local variables are ideal as temporary storage space when a procedure is executing. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.
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:
B = 200
You represent date literals and time literals by enclosing them in number signs (#), as shown in the following example.
CutoffDate = #06/18/2008#
CutoffTime = #3:36:00 PM#