The Internet Encyclopedia (Volume 3)

(coco) #1

P1: JDT


WL040C-44 WL040/Bidgoli-Vol III-Ch-51 June 23, 2003 16:33 Char Count= 0


VBSCRIPTLANGUAGEFUNDAMENTALS 625

Figure 1: What the user sees when running either of the
scripts given in Listings 2 and 3.

IsNumeric() function which returns a Boolean value in-
dicating if data are or can be transformed into a numeric
variant subtype. Note that what the user sees is identical
regardless of which of these two scripts is executed.
Figure 1 shows what the user sees when running either
of the scripts given in Listings 2 and 3.

Listing 2:VBScript illustrating If...Then...Else program
flow control.
Option Explicit
Dim strMyVar, intMyVar

Do
strMyVar = InputBox_
("Enter a number between 1 and 3")
Loop Until IsNumeric (strMyVar)

intMyVar = CInt(strMyVar)
If intMyVar = 1 then
MsgBox ("You Entered the Lowest Option.")
ElseIf intMyVar = 2 then
MsgBox ("You Entered the Middle Option.")
ElseIf intMyVar = 3 then
MsgBox ("You Entered the Highest Option.")
Else
MsgBox_
("You Entered a Number Too Small or Too
Big!")
End If

Listing 3:Script illustrating Select Case program flow control.
Option Explicit
Dim strMyVar, intMyVar

Do
strMyVar = InputBox_
("Enter a number between 1 and 3")

Loop Until IsNumeric (strMyVar)
intMyVar = CInt(strMyVar)

Select Case intMyVar
Case 1
MsgBox ("You Entered the Lowest Option.")
Case 2
MsgBox ("You Entered the Middle Option.")
Case 3
MsgBox ("You Entered the Highest Option.")
Case Else
MsgBox_
("You Entered a Number Too Small or Too
Big!")
End Select

User-Defined Procedures
In addition to intrinsic functions, VBScript allows pro-
grammers to define their own functions and subroutine
procedures. User-defined functions, code blocks that be-
gin with the key word “Function” and end with an “End
Function” statement, return a single value. Since VB-
Script only allows variant data type, all functions return
a variant; therefore a data type is not included in a func-
tion declaration (as it typically is when declaring func-
tions in Visual Basic and VBA). User-defined subroutines,
code blocks that begin with the key word “Sub” and end
with an “End Sub” statement, do not return a specific
value, but rather perform tasks (including input, output,
and user dialog tasks) and may affect values of script-
level (i.e., global) scope variables. Variables and named
constants declared in functions and subroutines have a
procedure-level (i.e., local) scope and are not directly visi-
ble to the calling routine. (However care must be taken not
to try to use a script-level variable with the same name as
a variable local to a particular subroutine or function.)
Local scope variable values set or changed during one
call to a subroutine or function do not carry over to sub-
sequent calls. Subroutines and functions may be called
recursively (i.e., may call themselves), though great care
must be exercised when doing so. By default, argument
variables needed by a user-defined subroutine or function
are passed by reference. This means a pointer to each ar-
gument variable in memory is passed. Any modifications
to an argument variable that happen in a subroutine or
function persist when control is passed back to the calling
procedure. A programmer may use the ByVal key word in
the subroutine or function declaration—e.g., “Sub MySub
(ByVal myVar)”—in order to override this default behav-
ior. Arguments passed by value remain unchanged when
control passes back to the calling procedure, regardless of
what is done in the subprocedure.

VBScript Coding Conventions
Over time, the community of programmers using any
given language tends to develop conventions in areas such
as naming patterns for variables and constants, the way
program commentary is incorporated, and the way code is
formatted and indented. While there is no compulsion to
Free download pdf