home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Because an object’s methods are properties, you can easily add new properties to an object by writing your own function and creating a new object property using your own function name. If you want to add a Bill method to your customer object, you can write a function named BillCustomer and set the object’s property as follows:

customer.Bill = BillCustomer;

To call the new method, you write the following:

customer.Bill()

VBScript Language Elements

Although VBScript is not as flexible as C++ or Visual Basic, it is quick and simple. Because it is easily embedded in your Web pages, adding interactivity or increased functionality with a VBScript is easy—a lot easier than writing a Java applet to do the same thing. (Although, to be fair, you can do a lot more with Java applets.) This section covers some of the nuts and bolts of VBScript programming.

A full language reference for VBScript, as well as Microsoft’s tutorial for VBScript programming, is included on the accompanying CD-ROM. Because VBScript is a new and evolving language, you can get up-to-the-minute information on it at the Microsoft VBScript Web site at http://www.microsoft.com/vbscript/.

VBScript Variables

All VBScript variables are of the type variant, which means that they can be used for any of the supported data types. Table 22.1 summarizes the types of data that VBScript variables can hold:

Table 33.1 Data Types that VBScript Variables Can Contain

Type Description

empty Uninitialized and is treated as 0 or the empty string, depending on the context
null Intentionally contains no valid data
Boolean true or false
byte Integer in the range –128 to 127
integer Integer in the range –32,768 to 32,767
long Integer in the range –2,147,483,648 to 2,147,483,647
single Single-precision floating point number in the range –3.402823E38 to –1.401298E-45 for negative values and 1.401298E-45 to 3.402823E38 for positive values
double Double-precision floating point number in the range –1.79769313486232E308 to –4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values
date Number that represents a date between January 1, 100 to December 31, 9999
string Variable-length string up to approximately 2 billion characters in length
object Any object
error Error number

Forming Expressions in VBScript

An expression is anything that can be evaluated to get a single value. Expressions can contain string or numeric variables, operators, and other expressions, and they can range from simple to quite complex. The following expression uses the assignment operator (more on operators in the next section), for example, to assign the result 3.14159 to the variable pi:

pi=3.14159

By contrast, the following is a more complex expression whose final value depends on the values of the two Boolean variables Quit and Complete:

(Quit = TRUE) And (Complete = FALSE)

Using VBScript Operators

Operators do just what their name suggests: They operate on variables or literals. The items that an operator acts on are called its operands. Operators come in the following two types:

  Unary—These operators require only one operand and the operator can come before or after the operand. The Not operator, which performs the logical negation of an expression, is a good example.
  Binary—These operators need two operands. The four math operators (+ for addition, – for subtraction, × for multiplication, and / for division) are all binary operators, as is the = assignment operator you saw earlier.

Assignment Operators Assignment operators take the result of an expression and assign it to a variable. One feature that VBScript has that most other programming languages do not is the capability to change a variable’s type on-the-fly. Consider the example shown in Listing 33.1.

Listing 33.1 Pi-fly.htm—VBScript Variables Can Change Type On-the-Fly


<HTML>
<HEAD>
<SCRIPT LANGUAGE=”VBS”>
<!-- Hide this script from incompatible Web browsers!
Sub TypeDemo
    Dim pi
    document.write(“<HR>”)
    pi = 3.14159
    document.write(“pi is “ & CStr(pi) & “<BR>”)
    pi = FALSE
    document.write(“pi is “ & CStr(pi) & “<BR>”)
    document.write(“<HR>”)
End Sub
<!-- -->
</SCRIPT>
<TITLE>Changing Pi on-the-Fly!</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
If your Web browser doesn’t support VBScript, this is all you will see!
<SCRIPT LANGUAGE=”VBS”>
<!-- Hide this script from incompatible Web browsers!
TypeDemo
<!-- -->
</SCRIPT>
</BODY>
</HTML>


NOTE:  The Cstr function converts a numerical value into a string.

This short function first prints the (correct) value of pi. In most other languages, however, trying to set a floating point variable to a Boolean value either generates a compiler error or a runtime error. Because VBScript variables can be any type, it happily accepts the change and prints pi’s new value: false (see Figure 33.1).

The assignment operator, =, assigns the value of an expression’s right side to its left side. In the preceding example, the variable pi gets the floating point value 3.14159 or the Boolean value false after the expression is evaluated.

Math Operators The previous sections gave you a sneak preview of the math operators that VBScript furnishes. As you might expect, the standard four math functions (addition, subtraction, multiplication, and division) work the same as they do on an ordinary calculator and use the symbols +, –, ×, and /.


Figure 33.1  Because all VBScript variables are of type variant, not only can their values be changed, but also their data types.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.