By Paul Lomax, Matt Childs, Ron Petrusha
Cover | Table of Contents | Colophon
Const statement
to allow user-defined constants. Finally, the
CreateObject and GetObject
functions were added to instantiate external COM objects; these
functions, which are inoperative in a client-side scripting
environment, are essential for supporting components that are capable
of extending a scripted server-side application.Sub...End Sub
construct is used to define a subroutine; that
is, a procedure that performs some operation but does not return a
value to its calling program. Blocks of code defined as subroutines
with the Sub...End Sub construct can be called in
two ways:Class...End
Class construct is
initialized, its Initialize event, if one has been defined, is
executed automatically. For subroutines of this type, the
routine's name can be constructed in only one way, as follows:Sub...End Sub
construct is used to define a subroutine; that
is, a procedure that performs some operation but does not return a
value to its calling program. Blocks of code defined as subroutines
with the Sub...End Sub construct can be called in
two ways:Class...End
Class construct is
initialized, its Initialize event, if one has been defined, is
executed automatically. For subroutines of this type, the
routine's name can be constructed in only one way, as follows:Sub objectname_event
Class...End
Class
construct. The syntax of the
Class statement is:Class classname
<% or <%= tags and
terminated by the %> tag. (For details on how
script is embedded within in ASP page, see Chapter 5.) This code is executed automatically as the
page's HTML is parsed.<SCRIPT>...</SCRIPT>
tags in an ASP. However, this is not
genuine script-level code: aside from variable declarations, the
order in which this code is executed is undefined.<SCRIPT> tag, the
variable declaration is recognized but the variable assignment
isn't. We can determine this because we've used the
Option
Explicit statement to
require variable declaration, but the VBScript language engine did
not raise an error when it first encountered the use of
x on the second line after the
#include
server-side directive. Its syntax is:<!-- #include PathType = sFileName -->
File
Virtual
Dim var1, var2 var2 = 0
Dim statement on
the first line (which declares a variable; it is discussed later in
this chapter in Section 3.2.5) and
the assignment statement on the second line. In addition, a
variable's subtype is Empty if it has been explicitly assigned
a value of empty, as in the following code fragment:Dim var1 var1 = Empty
Dim var1, var2 var2 = 0
Dim statement on
the first line (which declares a variable; it is discussed later in
this chapter in Section 3.2.5) and
the assignment statement on the second line. In addition, a
variable's subtype is Empty if it has been explicitly assigned
a value of empty, as in the following code fragment:Dim var1 var1 = Empty
<HTML>
<HEAD><TITLE>Error Checking</TITLE>
<BODY>
<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
Dim n, x
n = 10
x = Request.Form.Item("txtNumber")
If x = 0 Or Not IsNumeric(x) Then
Response.Write "x is an invalid entry"
Else
y = n / x
Response.Write y
End If
</SCRIPT>
</BODY>
</HTML>
If...Else statement and had allowed
any value to be assigned to x. Sooner or
later, some user will fail to enter a value or will enter a zero.
Possibly in both cases, and certainly in the latter case, this would
generate the "cannot divide by zero" runtime error. So
error handling, as this code fragment illustrates, is as much about
careful data validation as it is about handling actual errors.On
Error statement,
which informs the VBScript engine of your intention to handle errors
yourself, rather than to allow the VBScript engine to display a
typically uninformative error message and halt the program. This is
done by inserting a statement like the following at the start of a
procedure:On Error Resume Next
On Error Resume Next x = 10 y = 0 z = x / y Alert z
On Error
statement in line 1, program execution continues with line 5. The
problem with this is that when an error is generated, the user is
unaware of it; the only indication that an error has occurred is the
blank Alert box (from line 5) that's displayed for the user.On
Error statement
is valid until another On
Error
statement in the line of execution is encountered. This means that if
Function A contains an On Error statement, and
Function A calls Function B, but Function B does not contain an
LANGUAGE as
LANGAUGE in coding the
<SCRIPT> tag. The result was that MSIE
immediately began reporting JavaScript syntax errors. This
isn't surprising, given that in the absence of a valid
LANGUAGE attribute, MSIE used its default
scripting language, JScript. But when confronted with this situation,
it takes a while to recognize the obvious—that the
LANGUAGE attribute for some reason is improperly
defined; instead, it seems that Internet Explorer and VBScript are
somehow mysteriously "broken." The only real way to
reduce the time spent scratching your head is to build code in small
executable stages, testing them as you go. Another good tip is
to use individual small sample scripts if you are using a function or
set of functions for the first time and aren't sure how
they'll work. That allows you to concentrate on just the new
functions rather than on all the rest of the script as well. And
perhaps the most effective technique for reducing troublesome
misspelling of variables is to include the |
Name
|
Description
|
|---|---|
|
Contents Collection
|
Contains all application-scoped variables and objects added by script.
|
|
Lock Method
|
Locks the Contents collection, preventing other instances from
accessing it until it is unlocked. Its syntax is
Application.Lock( ).
|
|
OnEnd Event
|
Function Item_Open( ) End Function
Const,
Dim, Private,
Public, and Declare
statements). In Outlook, it can contain a far larger range of
statements; object assignments and access to the Outlook object
model, however, tend to be problematic if their code is placed here.
All variables defined at the script-level, regardless of whether they
are defined using the Dim,
Private, or Public keywords,
are public to the script.
Item keyword. In
other words, as your script is executing, the Item object is the
current object; to access other objects in the object model, you have
to navigate to them from the Item object.MsgBox Item.Application.Version
MsgBox Application.Version
Dim objPage Set objApp = Item.Application