VBScript Variable NamingPritesh
1. VBScript Variables :
- Variable is memory placeholder which holds the value that can be changed during the execution of the script
- Variable value can be changed during execution.
- Variable naming convension rules are used to define and declare variables.
- VBScript supports only one data type type i.e Variant
2. Different Rules for Declaring Variables :
- Variable name must begin with an alphabet.
- Variable name cannot exceed 255 characters.
- Variable name should not contain space,tab and special characters.
- Variable name should not contain period(.)
- Variable name should be unique in the declared context.
- 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.
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 :
- The numeric values should be declared without double quotes.
- The String values should be enclosed within double quotes(“)
- 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#
|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#|