VBScript Dim to Declare Variable
In VBScript Dim statement is used to declare variables. VBScript Dim automatically declares the variable as string type when a string value is passed to it enclosed in double quotes "". If number is assigned to the variable without enclosing it in double quotes then VBScript Dim declares the variable as integer type. A variable defines a name for memory location that can store values during the execution and runtime of script. Following are the uses of variables in vbscript:
1. To store the value entered by the user.
2. To store the value retrieved from database.
3. To store the value returned by the functions
4. To store the value of result obtained after manipulations or calculations.
In VBScript you can declare the variables using Dim, Public or Private statement.
Dim firstName ' Or Public firstName ' Or Private firstName
VBScript variables are of variant type that can store data of any type. If value stored in the vbscript variable is string then variable works as string type and similarly for other data types such as integer, Boolean, double etc.
VBScript Dim and Option Explicit
VBScript allows implicit declaration of variables in the script code that enables you to declare a variable without Dim statement.
firstName = "demoName"
Above vbscript code will declare a variable of string type with name firstName implicitly. But this is a bad habit of coding coz typographic mistakes results in suspected output. For example if in the vbscript code you want reuse the variable firstName to concatenate the lastName with it then wrong name for firstNeme with letter "e" second time will not show the value stored in previous variable firstName. VBScript will declare it as new variable as firstNeme.
To store the values in relevant variables declared in the vbscript code you can use "Option Explicit" at the top of vbscript before declaring global variables. Option Explicit keeps check on variable names declared implicitly without Dim statement.
Example of VBScript Dim to Declare Variable
Dim firstName firstName = "Demo" Dim count count = 10
Above VBScript code will declare the variable firstName as string type because of string value assigned to it enclosed in double quotes e.g. "Demo". Variable count in the above vbscript code has been assigned a number value that declares it as integer type e.g.: count=10. As in other programming languages, in VBScript equal to "=" sign is used to assign the value to the variable. Value at right side of "=" sign is assigned to the variable at left side.