When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Using Option Explicit


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:

<%
MyProgramName = "myfile.asp"
Response.Write(MyPrograName)
%>

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:

<%
Option Explicit 'Always, always, always! :)

MyProgramName = "myfile.asp"
Response.Write(MyPrograName)
%>

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:

<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<!--#include virtual="/scripts/myaspfile.asp"-->
<%   
'Now do your ASP stuff...
%>

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:

<%

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=Northwind"
%>

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 http://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.

Furthermore, Option Explicit also increases the performance of your ASP pages! As documented in the article Enhancing Performance in ASP Pages, using 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 


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article