VBScript Dim to Declare Variable

Updated on 20 Feb 2012,
Published on 25 Jul 2008

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.

Example
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.

Example
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.

2 Responses to "VBScript Dim to Declare Variable"
Tendai
Hi,
I want to assign a variable a size, i do not want to use the defaults by VBscripts. This is causing a lot of problem because when the variable value hits a trillion(ie 1 000 000 000 000 ) it is automatically converted to a double and its is exponentiated (ie 1*10e9) of which i do not like to view the figure like that. I want to declare the variable and give it a specific data type like say, currency (and give it specific size)so that it wont be exponentiated and its relevant to my information needs. Please help how do i go about this.
Ezineasp.net
Hi Tendai

You can use [b]Vbscript FormatCurrency[/b] or [b]FormatNumber[/b] function to display the numerical value. These vbscript functions accept the first argument as number (pass your variabeName) and second argument as number of digits after decimal point.

E.g.:

Dim a
a = 1000000000

Response.Write( FormatCurrency(a,2))

[b]Output:[/b]

$1,000,000,000.00


[b]Or[/b]

Response.Write( FormatNumber(a,2))

[b]Output[/b]

1,000,000,000.00


Hope this will work for you.

Good Luck.
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email