When writing your ASP pages, there is one very important steps many developers, unfortunately, forget to do. And that vital step, if you cound't tell by the title, is to use Option Explicit.
Option Explicit requires that all variable names be defined (with the Dim statement). To learn more ASP basics, such as how to declare variables and the variant nature of variables, be sure to check out the 4Guys ASP Beginners FAQ!
It is good to define your variables for a number of reasons. First, let's look at this code example:
This will print out nothing! Since I mis-spelled MyProgramName in the Response.Write (I forgot the "m"), ASP will create a new variable named MyPrograName and assign a blank string to it. Simple typos like this can cause incredible headaches and hour long debugging sessions for something that should have never happened!
By writing your code like this:
Now we will get an ASP error informing us that MyPrograName is undefined. This is a LOT easier to debug and should cause us developers less headaches! For Option Explicit to work properly, it must be the first ASP statement. If you include files containing ASP, you will need to put the Option Explicit before your include the file, like this:
Be aware that you will need to declare your variables in the ASP script or before the include file. For example, say your include file contains the following code:
In theory it should contain a "Dim conn" before the "Set conn..." statement. If it doesn't, you will get an undefined variable error in the ASP file using Option Explicit and including the file. This is because including a file is synonymous to having typed in the text file's contents directly into your ASP page. For information on including files, be sure to visit https://www.4guysfromrolla.com/webtech/faq/Beginner/faq6.shtml.
Another good reason to use Option Explicit is that if you do, you will be following a good set of coding conventions. Coding conventions are extemely important for maintainability and readability in all languages, and it is essential that you try your darndest to adhere to some sort of covention. About a month ago I wrote an article on coding conventions for ASP, you can access it via this URL.
Option Explicit also increases the performance of your ASP pages! As documented in
the article Enhancing Performance in ASP Pages,
Option Explicit in your ASP pages can result in performance increases of 10%!
Well, I hope this FAQ has convinced at least some of you to always use Option Explicit! Have a great day! If you have any questions, be sure to reply to this email with your questions / comments.
|FAQ Table of Contents||Passing Variables Between ASP Pages using POST||Using Includes|