VBScript Variable Naming



1. VBScript Variables :

  1. Variable is memory placeholder which holds the value that can be changed during the execution of the script
  2. Variable value can be changed during execution.
  3. Variable naming convension rules are used to define and declare variables.
  4. VBScript supports only one data type type i.e Variant

2. Different Rules for Declaring Variables :

  1. Variable name must begin with an alphabet.
  2. Variable name cannot exceed 255 characters.
  3. Variable name should not contain space,tab and special characters.
  4. Variable name should not contain period(.)
  5. Variable name should be unique in the declared context.
  6. Variable name can be short but it is good practice to provide descriptive name to variable.

3. Declaring Variable :

Step of creating variable in VBScript is referred as Variable Declaration and VBScript variables are declared with Dim keyword.

VBScript variables can have access specifiers associated with it i.e public and private

Dim num
Dim numOfRecords

In the above example we have declared two variables num and numOfRecords.

Option Explicit :

If we mispelled the above variables in our code like this –

numofrecords = 29

then VBScript will automatically create new variable. In order to prevent VBScript to create new variable we use following syntax –

Option Explicit
Dim num
Dim numOfRecords

Option Explicit statement forces you to declare all your variables with the dim, public or private statement.

Multiple Variables :

If we need to declare multiple variables then we can use comma to separate multiple variable declarations.

Dim Var1,Var2

4. Assigning Value to Variable :

like any other scripting language we can simply assign the value to the variable using the assignment operator (=) symbol.

numOfRecords = 10

will assign value 10 to the numOfRecords. The variable name on the left hand side followed by an equal to (=) symbol and then its value on the right hand side.

Different rules :

  1. The numeric values should be declared without double quotes.
  2. The String values should be enclosed within double quotes(“)
  3. The Date and Time variables should be enclosed within hash symbol(#)

Consider the following example :

num = 25
StrName = "VBScript"
dateOfBirth = #02/12/2013#
timeOfExecution = #12:13:14 PM#

Summary :

Type of Variable Rule for use Example Value
Integer Use without double quote 25
Integer Use with double quote VBScript
Integer Use with hash #02/12/2013#