variables in VBScript
variables in VBScript
Variables
Before you use a variable in VBScript you should declare its name . To do this in VBScript you use the dim keyword like the following examples.
You should follow a few rules when you declare variable names and these are
- must begin with a letter
- cannot contain a period (.)
- cannot have more than 255 characters
dim x
dim y
dim myAge
dim myWage
You could have put these all on the same line like this
dim x , y , myAge , myWage
The initial value of the variable is empty in VBScript
To prevent mistakes when declaring variables you can use Option Explicit before declaring them and this means you must declare all your variables with dim , public or private , if you dont you will get an error . Anyone who has created scripts in JavaScript will see the advantage here where you can declare a variable like this
myvar
but if you make a spelling mistake like myyar later on this will not be flagged and can lead to some strange and hard to find errors.
Types of Variables
In VBScript a variable can hold any a value of any data type and this data type does not have to be set when the variable is declared . This means you can change the data type of your variable in your program .
| Datatype | Description |
| Empty | Variant is uninitialized . Value is 0 for number variables and a zero length string “” for empty strings |
| Null | Variant which contains no valid data |
| Boolean | either True or False |
| Byte | integer in the range 0 to 255 |
| Integer | integer in the range -32768 to 32767 |
| Currency | -922,337,203,685,477.5808 to 922,337,203,685,477,5807 |
| Long | an integer value in the range -2,147,483,648 to 2,147,483,647 |
| Single | single precision floating point number in the range of 3.402823E38 - 45 to 1.401298E - 45 for negative values and 1.401298E - 45 to 3.402823 E 38 for positive values |
| Double | a double precision floating point number |
| Date/Time | a number that represents the date between Jan 1 100 to Dec 31 9999 |
| String | a variable length string that can contain 2 billion characters |
| Object | contains an object |
| Error | contains an error number |























