P1: JDT
WL040C-44 WL040/Bidgoli-Vol III-Ch-51 June 23, 2003 16:33 Char Count= 0
626 VISUALBASICSCRIPTINGEDITION(VBSCRIPT)follow such conventions, doing so tends to improve read-
ability and sharability of code. There are several recog-
nized coding conventions used by VBScript programmers
(Microsoft, 2002c). Two naming patterns are commonly
used for named constants. One pattern specifies all upper
case for names of constants. When using this pattern, indi-
vidual words within the name of a constant are delineated
using an underscore (thus, “MYCONSTANTNAME”).
An alternative approach is to prefix names of constants
with “con” and use a mixed case pattern where the first
letter of embedded words is upper case (thus, “conMy-
ConstantName”). This latter approach follows from vari-
able naming conventions, where a three-letter prefix is
used to indicate the variant subtype of the variable (i.e.,
strMyStringVariable). Sometimes an additional character
(“s”) is prefixed to names of variables having script-level
scope (thus, sintMyIntegerVariable). User-defined subrou-
tine and function names are also typically mixed case and
usually begin with a verb describing what the procedure
does—e.g., “Sub FetchEnvironmentValues ()”.
Lines prefaced by either the string “REM” or a sin-
gle apostrophe are interpreted in VBScript as in-program
comments and are not processed by the VBScript inter-
preter. A single apostrophe following a VBScript state-
ment is also interpreted as the start of a comment. To use
the REM key word on the same line following a VBScript
statement, the REM key word must be preceded by a
colon. (A colon is the delimiter used to allow multiple VB-
Script statements on a single line.) All text following either
the key word REM or a single apostrophe is ignored by the
scripting engine to the end of the current line. In-program
comments not only should be descriptive, but also should
focus on what rather than how, in order to minimize com-
ment maintenance. Thus each procedure or subprocedure
should begin with an extended comment describing the
purpose of the procedure, its required input(s), any out-
puts, any assumptions about inputs and outputs, and its
return and/or the global variables effected. A minimum
should be said about how it accomplishes these tasks since
that information is typically more volatile.
When scripts are created, indenting should be used to
show flow control and logical structure of the code. Thus
blocks of code within an If... Then... Else statement (and
similar flow control statements) will be indented relative
to the If and Else lines themselves. To conserve screen
space and allow for nesting of code blocks, a typical indent
is only three to five spaces.
Finally, though not required by the language, many
programmers find it useful to declare (using Dim state-
ments) all variables and named constants before use.
Starting a script off with the line “Option Explicit” re-
quires that all variables and named constants used in that
script be declared before first use. If a typo in a name is
made in the code, having option explicit in effect will im-
mediately highlight the error, which can greatly facilitate
code debugging.OBJECTS IN VBSCRIPT
Use of Objects in VBScript
Objects are abstract structures used in high-level pro-
gramming languages to encapsulate discrete modules ofcode and associated data structures. They enable modular
implementation and reuse of code and facilitate collab-
orative development of advanced applications. When the
archetype of a programming object, called an object class,
is written, standard types of programming interfaces and
class behaviors are defined that allow instances of objects
created from the class definition to be used easily and
reliably in many different programs. Instead of writing
their own code to perform common tasks, programmers
can use classes as templates to instantiate (create) object
instances for use in their programs. Programmers imple-
menting a predefined class can treat an object instance
created from that class like a “black box.” A programmer
using an object class does not have to understand how the
class works internally. All he or she has to understand is
how to use the interfaces and behaviors of the class. In-
terfaces and behaviors of external object classes invoked
using VBScript are described in terms of properties, meth-
ods, and events. Object classes written in more advanced
object-oriented programming languages (e.g., C++) may
also exhibit polymorphism and inheritance. These latter
two behaviors are not supported in VBScript; however,
they are supported in Visual Basic.NET.
VBScript provides support for the use of registered
ActiveX object classes, also referred to as object linking
and embedding server objects and COM or COM+ objects.
While VBScript provides a language intrinsic CreateOb-
ject method for instantiating a new instance of an object
and a language intrinsic GetObject method for creating
additional references to an already instantiated object, it
is usually better when possible to invoke equivalent meth-
ods built into an application-specific object model pro-
vided by the scripting host being used. This allows the
scripting host to track the use of the object and resources
consumed by it and integrate the object into the host ap-
plication environment. Thus, when a script is written for
the WSH, the preferred syntax to create an instance of an
Active Data Object (ADO) Recordset object would beSet objMyRecordset = WScript.CreateObject
("ADODB.Recordset")Note that the string argument of the WScript object’s Cre-
ateObject method is the ProgID of the object, typically
consisting of the library or ActiveX server name and the
type name (class name) of the object being instantiated.
ProgIDs are maintained in the HKEYCLASSESROOT
key of the host system’s registry. Note the use of the “Set”
key word in the statement that assigns the reference for
the object to the variable objMyRecordset. This is how
VBScript knows you are assigning an object reference to
a variable. When finished with an object, you should use
the “Nothing” key word to free resources assigned to the
variable; thus,Set objMyRecordset = NothingNote also that VBScript uses dot syntax for refer-
encing an object’s methods and properties (includ-
ing collections of properties) and underscores syntax
for referencing an object’s events. Thus in this exam-
ple WScript.CreateObject() referenced the CreateObject