- The Fundamentals of VBScript
- VBScript Versus VB
- Creating Variables in VBScript
- Concatenating Strings
- Arrays and Loops
- Resizing Arrays
- Inequality Operators
- Conditional Statements
- Select Case Statements
- Sub
- Function
- Working with Arguments
- Beware of Types
- Event Procedure Naming Syntax
- Server-Side Events
- Local Variables
- Script-Level Variables
Beware of Types
Consider the following sample of code from Listing 3.10.
'Get the value from the first argument x = CInt(ArgOne) 'Get the value from the second argument y = CInt(ArgTwo) If x > y Then GetBiggerNumber = x Else GetBiggerNumber = y End If
You might be wondering why the VBScript conversion function CInt() is used. CInt() converts string values that look like numerals into actual integers. Although it is true that all variables declared in VBScript are Variants, and Variants can represent any data type known to man, things are not always as easy or as universal as they seem.
Things get a little murky when data is assigned to the arguments. If the Variant ArgOne contains the value 12 and ArgOne decides to be a string, and the Variant ArgTwo contains the value 5 and decides to be a string also, the function returns the value 5. Mathematically the numeric value 5 is definitely not greater than 12. However in terms of string comparisons, 5 is greater than 12. It's the way strings work.
Therefore, to avoid this sort of inaccuracy, we need to be able to tell the Variants to interpret its value as an Integer. This is where the VBScript CInt() function comes in handy. When we apply the function to the Variant ArgOne, we are telling the variant to consider ArgOne to be an Integer. The process is called typecasting. We are transforming a variable from type Variant to type Integer.
VBScript provides a variety of type conversion functions. Table 3.4 shows you the ones most commonly used.
Table 3.4: Often-Used VBScript Conversion Functions
Function |
Description |
CInt(var) |
Converts the variable to an Integer |
CStr(var) |
Converts the variable to a String |
CDbl(var) |
Converts the variable to a Double |
CLng(var) |
Converts the variable to a Long |
CCur(var) |
Converts the variable to Currency |
CBool(var) |
Converts the variable to a Boolean (True/False) |
CDate(var) |
Converts the variable to a Date |
There will be many times when you will need to ensure that your variables are of a specific type, particularly after you assign ambiguous values to a variable. You'll find conversion functions to be a handy tool to help you ensure your data's integrity.
There are a special group of procedures that you create that are executed automatically by the browser or Internet Information Server when an event is fired. These procedures are called event procedures.
An event procedure is code that you program, and that is run in response to a given event that is fired within a program, object or Web page. The event procedure you are probably most familiar with is the OnClick event, which is associated with the Click event of a button. Whenever a user clicks on a button on a Web page in a browser, a Click event is fired by the browser. After the event is fired, the browser will go off to look for an OnClick event procedure within the Web page's <SCRIPT> tags. If the event procedure exists, it is executed. If none is found, the browser goes on about its business. The same dynamic is in force for Internet Information Server when server side events are fired.
Most of the common events have an event procedure associated with each particular event. For instance, Click events are associated with OnClick event procedures. MouseMove events are associated with OnMouseMove event procedures, MouseDown events are associated with OnMouseDown event procedures, and so forth.
In VBScript you must create the event procedure for the event that you want to capture. This is very unlike standard Visual Basic where the Visual Basic IDE creates the skeleton of a given event's event procedure code for you. It's a little tricky to get used to doing all this from scratch. However, it is a skill easily mastered with a little practice.
Caution
If you do not name an event procedure just right, you might be in for a surprise. The code will not produce an error. It simply will be ignored by the browser or IIS as just another procedure sitting in a set of <SCRIPT> tags.