P1: JDT
WL040C-44 WL040/Bidgoli-Vol III-Ch-51 June 23, 2003 16:33 Char Count= 0
VBSCRIPTLANGUAGEFUNDAMENTALS 623“Const,” remain fixed throughout script execution, and
are typically initialized in a declaration statement, e.g.,Const MyConst = "This is an unchanging
string."VBScript variables are declared using the any of the key
words “Dim,” “Public,” or “Private.” (The key words Pub-
lic and Private are most often chosen to declare variables
in script modules that are to be used as objects by other
programs or scripts as discussed later.) VBScript supports
implicit declaration of scalar variables; i.e., it does not re-
quire that scalar variables be declared before first use.
Both fixed array variables and dynamic array variables
(i.e., arrays whose sizes may be changed during program
execution) are supported in VBScript; however, unlike Vi-
sual Basic and VBA, the base value of all array indices
is always zero and cannot be changed. Care must also be
taken in VBScript when assigning arrays as values of items
in an object collection. Object collections are themselves
implemented in a fashion reminiscent of arrays, resulting
in syntactical ambiguities when one tries to reference a
specific element of an array that is itself an item in an
object collection. (The solution is to assign in its entirety
the collection item that is an array to another, stand-alone
array variable, and then to access individual array items
through that stand-alone array variable. The stand-alone
array variable can be reassigned to the collection item
variable after any value changes.) Unlike Visual Basic and
VBA, VBScript does not support user-defined structured
data types. VBScript variable and constant names are not
case sensitive and must not be reserved words. VBScript
includes a number of intrinsic constants (e.g., vbCrLf,
which is the standard PC end-of-line 2-byte string) all of
which begin with the letter sequence “vb.” These constants
cannot be reassigned nor may their names be used as vari-
able names (e.g., the statement vbCrLf=“hello” will gen-
erate a run-time error). Intrinsic constants are provided
for output display color values (e.g., vbBlack), comparisonoperations (e.g., vbTextCompare to do a case-insensitive
text comparison), date values (e.g., vbSunday), and string
and character values (e.g., vbTab). Programmers may also
import constant declarations (e.g., when working with
supplemental or third-party software libraries). Finally,
VBScript variables and named constants do have scope;
i.e., variables declared within a function or subprocedure
are local in scope and are not accessible to code out-
side that function or subprocedure. In applications where
script modules are to be used as objects by other programs
or scripts, variables also have public (variable may be ac-
cessed by applications invoking the script module as an
object) or private (variable is only available within the
script module in which it is declared) scope.
Because computers can store a variety of data in a vari-
ety of different formats and representations, each variable
or named constant typically has associated with it a data
type. The data type defines the kind of data (e.g., num-
ber, character, logical, and object reference) and often its
structure (e.g., 2-byte integer, multicharacter string, and
integer array). Unlike Visual Basic and VBA, however, VB-
Script recognizes only one primary data type, which is
called a variant. Variant is a special chameleon-like data
type that can be used for any type of data, even including
objects. A variant is actually made up of two components,
one containing the data itself (or a pointer to the data,
e.g., in the case of objects, multibyte numbers, strings,
and arrays) and a second part, called the variant subtype,
indicating the representation of the data stored, i.e., its
effective data type. A variant can be used at one point
in a procedure to store an integer number and then used
later in the same procedure to store a string (though doing
this is usually bad practice). Table 2 lists available variant
subtypes.
Variants are useful shortcuts when scripting because
they let the scripting engine worry about the optimal rep-
resentation in the storage of an item of data. However, be-
cause variants encourage programmers to ignore variable
data types when authoring code, it is easy to inadvertentlyTable 2VBScript Variant SubtypesVariant Subtype Comment
Empty Variable has been declared but has not yet been initialized.
Null Contains no data (as result of an explicit assignment or assignment to result of an expression
involving other null variables).
Boolean Contains True or False.
Byte Contains 8-bit value (i.e., between 0 and 255).
Integer Signed whole number in 2 bytes (i.e., between –32,768 and 32,767).
Long Signed whole number in 4 bytes (i.e., between –2,147,483,648 and 2,147,483, 647).
Single Signed, single precision floating point number (i.e., can be used to express nonwhole real numbers)
expressed in 4 bytes.
Double Signed, double precision floating point number (8 bytes).
Currency Signed, fixed decimal number (8 bytes) used for storing currency values.
Date / Time Contains date and time between January 1, 100 and December 31, 9999.
String Variable length string of characters (up to 2 billion characters in length).
Object Contains reference to an object.
Array Contains reference to an array.
Error Contains error number (such as generated by scripting engine).