Working With Variables

Top  Previous  Next

 

Variables - Working with Variables

Description:  Most modern programming languages use the concept of variables, items that are used as placeholders or storage for values and information, VBScript is no different.

Naming Restrictions
VBScript enforces some standard rules that apply to variable names:

They must begin with an alpha character (A...Z)
The must be less than 256 characters in length
They can not contain embedded periods (".")
They are NOT case sensitive (i.e. "myString", "MYSTRING" and "MyString" all refer to the SAME Variable)
Must be unique in the scope in which it is declared

Variable Types Since not all data is the same VBScript includes a range of variable types for storing different types of data. The table below summarizes the types available.

Boolean Contains either True or False

Byte Contains integer in the range 0 to 255

Currency Floating-point number in the range -922,337,203,685,477.5808 to 922,337,203,685,477.5807

Date(Time) Contains a number that represents a date between January 1, 100 to December 31, 9999

Double Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values

Empty Uninitialized Variant

Error Contains an error number used with runtime errors

Integer Contains integer in the range -32,768 to 32,767

Long Contains integer in the range -2,147,483,648 to 2,147,483,647

Null A variant containing no valid data

Object Contains an object reference

Single Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values

String Contains a variable-length string that can be up to approximately 2 billion characters in length

 

Declaring Variables VBSCript supports both explicit and implicit variable declarations. To declare a variable explicitly you use the Dim keyworrd, for example:

Dim strVariable

strVariable = "This is a string"

You can also declare multiple variables by separating each variable name with a comma. For example:

Dim name, url, email

You can declare a variable implicitly by simply using its name in a script, without first using the Dim keyword.

Although declaring a variable implicitly may seem simpler it can lead to problems if one or more variables are misspelled. This could lead to unexpected results when the script was run, because the variable names would not be checked to make sure they are valid.

The use of the Option Explicit statement forces all variables to be explicitly declared before use.

Example:

Option Explicit

Dim strVariable 'declare the variable

strVariable = "This is a string" 'assign a value

SMEvent.Raise "Trace", strVariable 'display the variable 

'This is OK

Bad Example:

Option Explicit

strVariable = "This is a string" 'assign a value

SMEvent.Raise "Trace", strVariable 'display the variable 

'This would generate a run-time error because strVariable was not declared