What is variable?
Variable is a named container used to store values
Declaring a variable
There are three possible ways to declare a variable
Dim <VariableName>
Public <VariableName>
Private <VariableName> |
Ex –
Dim x, y, z
Dim a
Dim b, c |
Variable naming rules
Variable names should following the below rules
- Must not exceed 255 characters.
- Must be unique in the scope in which it is declared.
- Must begin with an alphabetic character.
- Cannot contain an embedded period.
Though these are the naming conventions specified by Microsoft. But we will be breaking the last 2 rules later in this article
Assigning values to variables
Variables can be assigned values why using the “=” to operator. Below code shows how different types of values can be assigned to a variable
Dim myVar
myVar = "Assigning a string value"
'Assigning a integer value
myVar = 2
'Assigning a double value
myVar = 3.3 |
Data Type of variables
All variables declared in VBScript are of Data type VARIANT. A VARIANT data type can store any types of value listed below
- Boolean
- Byte
- Date (Time)
- Double
- Error
- Integer
- Long
- Object
- Single
- String
All actual type of the data stored in a VARIANT type is known as the sub-type of the variable.
Declaring constant variable
Constants can be declared using below syntax
Const <ConstName> = <ConstValue> |
Ex –
Const APP_ NAME = "Testing VBScript"
Const APP_VERSION = "1.01" |
It is advised to use all caps variable name when declaring a constant.
Variable Naming conventions
Since variables can store various types of values it is always a good practice to name them with a consistent convention
|
Data Type
|
Prefix 1
|
Prefix 2
|
Prefix 3
|
|
Boolean
|
b
|
b_
|
bln_
|
|
Byte
|
by
|
by_
|
byt_
|
|
Date (Time)
|
dt
|
dt_
|
dt_
|
|
Double
|
Dbl
|
dbl_
|
dbl_
|
|
Error
|
err
|
err_
|
err_
|
|
Integer
|
i
|
i_
|
int_
|
|
Long
|
l
|
l_
|
lng_
|
|
Object
|
o
|
o_
|
obj_
|
|
Single
|
sng
|
sng_
|
sng_
|
|
String
|
s
|
s_
|
str_
|
Above table shows few prefixes that can be used during scripting. You should choose the ones you like and stick to them. I personally prefer the Prefix 2 column. Below is the list of other Prefixes that might be required
- Function – fn
- Sub – sub
- Global variable – g
- Class member – m
- Class – cls
We would be discussing all above mentioned topics in the upcoming articles
Forcing variable declaration
VBScript allows a very poor programming practice which is not to declare the variable being used
Ex –
str_Message = "Tarun Lalwani"
Msgbox str_Message |

Msgbox for a undefined variable
Above code will work even when we haven’t declared the ” str_Message” variable using a Dim/Public/Private statement. VBscript when it looks at a variable name which is not yet declared it declares with an Empty value. In programming a typo when using the variable name is always a possibility. Ex –
str_Message = "Tarun Lalwani"
Msgbox str_Messge |

Msgbox for undefined and unused variables
The value above is empty message because we haven’t declared str_Messge variable and when used VBScript declares it with an empty value. Though it is pretty easy to spot the issue in above code but when the code size is huge, these issues become very difficult to debug as wrong variable name used at once place might cause an issue at different location of code. To avoid such a situation VBScript provides an “Option Explicit” statement which can be used at the top of the code as shown below
Option Explicit
str_Message = "Tarun Lalwani"
Msgbox str_Messge |
Now above code would throw an error

Undefined variable error when Option explicit is used
Breaking the 2 rules of naming restriction
Let us reiterate the last 2 rules for variable naming in VBScript
- Must begin with an alphabetic character.
- Cannot contain an embedded period.
Thought this is pretty much true the normal way
All below declaration will give syntax errors
Dim 1A
Dim _AB
Dim A B
Dim A.B |
Though the error might be different for above declarations but the source of the error is the same, bnot following the naming convention. Now VBScript provides a very less talked about square bracket “[]” declaration
Dim [9X]
Dim [_varName]
Dim [My Name]
Dim [this.A]
[My Name] = "Tarun Lalwani"
[this.A] = [My Name]
MsgBox [this.A] |

Breaking the variable declaration rules
please wait...
Rating: 8.0/10 (5 votes cast)