P1: JDT
WL040C-44 WL040/Bidgoli-Vol III-Ch-51 June 23, 2003 16:33 Char Count= 0
624 VISUALBASICSCRIPTINGEDITION(VBSCRIPT)generate data type mismatch errors when doing com-
parison operations and operations involving multiple
operands or arguments. To avoid uncertainty, it is possible
to ascertain the variant subtype of a variable and even to
force transformation of data stored in a variable from one
variant subtype to another. Variant (and named constant)
subtype can be determined by using either the TypeName
function or the VarType function (the former returns a
human-readable string indicating the subtype, the latter
a number code for the subtype). Values of variants can
be transformed (cast) using a variety of type-specific
functions, most of which begin with the letter “C” (e.g.,
CDate, CStr, and CInt). Listing 1 gives the code for a simple
VBScript file that can be run under WSH. This script will
generate three pairs of message boxes. The first message
box in each pair shows that the value of the variable
“myVar” is the numeral or number one. The second
message box in each pair shows the variant subtype of
myVar at each point in the program. (To run this
script yourself, create a plain text file on your computer
containing the code as shown and save it with the .vbs ex-
tension. Then double click on the file name from Windows
Explorer or enter the file name at the command prompt.)Listing 1:VBScript illustrating use of TypeName and CStr
functions.
Option Explicit
Dim myVarREM myVar as variant subtype String
myVar = "1"
MsgBox (myVar)
MsgBox (TypeName(myVar))REM myVar as variant subtype Intege
myVar = 1
MsgBox (myVar)
MsgBox (TypeName(myVar))REM myVar transformed from subtype Integer
to subtype String
myVar = CStr(myVar)
MsgBox (myVar)
MsgBox (TypeName(myVar))Essential Language Elements
VBScript statements are line delineated, i.e., one state-
ment per line unless special line continuation or statement
separator characters are used. For this reason no end of
statement character delimiter is used in VBScript.
VBScript provides a typical set of arithmetic opera-
tors for addition, subtraction, division, and multiplication
(+,−,/,∗), as well as special arithmetic operators for no
remainder integer division (\), remainder only modulo
division (Mod), and exponentiation (∧). The string con-
catenation operator is “&” (though where unambiguous
“+” can be used instead). Comparison operators are>,<,
<>,>=,<=, and=. Comparison operators can be
used to compare strings as well as numbers. “And,” “Or,”
“Not,” and “Xor” (exclusive Or) are used both as Boolean
logical operators and to perform bitwise operations. Inthe absence of parentheses, operators are evaluated left
to right in the following order: arithmetic (exponentia-
tion, division and multiplication, integer division, modulo
arithmetic, and addition and subtraction), concatenation,
comparison, and finally logical/bitwise operations (Not,
And, Or, and Xor).
VBScript version 5.6 provides a rich set of program
flow control constructs. Branching can be accomplished
using block-style If... Then... Else and Select Case state-
ments. Listing 2 shows a simple VBScript file that solicits
an integer between 1 and 3 from the user and branches ac-
cordingly using an If... Then... Else statement. Listing 3
does the same job using a Select Case statement. VB-
Script version 5.6 also supports flow control through
the use of several kinds of looping structures. VBScript
Do...Loop statements allow for either top or bottom
tested loops using either “until” or “while” conditionals.
Thus “Do While intMyValue>0... Loop” will execute the
included block of statements repetitively until the value
contained in the variable intMyValue becomes zero or less
than zero. The statement block will not be executed at all
if intMyValue is zero or negative when the Do statement
is first encountered. Conversely the block of statements
contained within “Do... Loop Until intMyValue>0” will
always be executed at least once and will be executed
repetitively until the value of intMyValue is positive. Er-
rors will arise if the variant subtype of intMyValue is or
changes to be nonnumeric. Also, unlike Visual Basic, VB-
Script does not support the “Do Events” statement, which
means that once inside a loop, scripts do not respond to
external events other than those checked explicitly within
the loop. If a coding error results in an endless loop this
means that script execution can only be terminated by ter-
mination of the scripting host application itself. Listings 2
and 3 illustrate a simple use of a “Do... Loop” construct.
VBScript also supports for...next and for each...next
loops. These types of loops are used to execute blocks of
statements a fixed number of times (for...next) or until
all the values in a given collection have been processed
(for each...next).
VBScript allows limited use of Visual Basic’s On Er-
ror statement to implement user-defined error handling.
Specifically, VBScript allows “On Error Resume Next” and
“On Error GoTo 0” forms of this statement. (No other uses
of On Error are supported in VBScript.) Most errors in
script statements that occur after an “On Error Resume
Next” statement and before the next “On Error GoTo 0”
statement will be left for the programmer to handle and
will not result in program termination. Information about
any errors that occur is available to the programmer via
the “Err” object (discussed later).
Finally VBScript includes a number of intrinsic func-
tions for manipulating data and interacting with the host
system. These are too numerous to describe in detail here,
but they include functions for obtaining system date and
time information, transforming variant subtypes, deter-
mining whether the content of a variable is or can be
meaningfully transformed to a particular variant subtype,
manipulating and calculating mathematical quantities,
manipulating date and time information (e.g., adding to
and subtracting from dates), and manipulating string and
character data. Listings 2 and 3 illustrate the use of the