There is much to be said for the old maxim, "The best way to learn is by making mistakes." Once you have made a mistake, understood what you did wrong, and rectified the error, you will in general have a much better understanding of the concepts involved and of what is needed to build a successful application. But to save you from having to experience this painful process of trial and error in its entirety, we'd like to share with you some of the most common errors that ourselves and other programmers we've worked with have made over the years. These types of errors are actually not unique to VBScript, nor in fact to VB, but to programming in general. In approximate order of frequency, they are:
Type mismatches by everyones favorite data type, the variant. Type mismatches occur when the VBScript engine is expecting data of one variant type like a string but is actually passed another data type like an integer.) Type mismatch errors are fairly uncommon in VBScript, since most of the time the variant data type itself takes care of converting data from one type to another. That tends, though, to make type mismatch errors all the more frustrating. For instance, in Example 4-5, if we hadn used the statements:
nNum1 = CDbl(Server.HTMLEncode(Request.Form("txtNum1"))) nNum2 = CDbl(Server.HTMLEncode(Request.Form("txtNum2"))) nQuot = CDbl(Server.HTMLEncode(Request.Form("txtQuotient")))
to convert the form data submitted by the user to numeric data, our application would not have functioned as expected. The best way to reduce or eliminate type mismatch errors is to adhere as closely as possible to a uniform set of VBScript coding conventions. (For a review of coding conventions and their significance, see Chapter 2.) For instance, when you know that a variable is going to hold a string, use a variable name like strMyVar to indicate its type, etc. Code becomes easier to use if you can tell instantly that some operation (like strMyString = intMyInt * dteMyDate) doesn make sense, but you e none the wiser if your line of code reads a = b * c.
Subscript Out Of Range is an error that occurs frequently when usingarrays. It actually doesn take much to eliminate this error for good. All you have to do is check the variable value you e about to use to access the array element against the value of the UBound function, which lets you know exactly what the maximum subscript of an array is.
The next most common error is division by zero. If you try to divide any number by zero, youll kill your script stone dead. While its very easy to generate adivision by zero error in a script, its also not at all difficult to prevent it. A division by zero error is easy to diagnose: whenever a variable has a value of zero, its likely to cause a problem. So all your script has to do is check its value and, if it turns out to be zero, not perform the division. Theres no rocket science here! Simply use an If x = 0 Thenconditional statement, where x is the variable representing the divisor.
VBScript with Active Server Pages
Part I: The Basics
Data Types and Variables
Error Handling and Debugging
VBScript with Active Server Pages
Programming Outlook Forms
Windows Script Host 5.6
VBScript with Internet Explorer
Windows Script Components
Part II: Reference
Part III: Appendixes
Appendix A. Language Elements by Category
Appendix B. VBScript Constants
Appendix C. Operators
Appendix E. The Script Encoder
VBScript in a Nutshell, 2nd Edition
Similar book on Amazon