Codefixer - ASP tutorials, resources and software
New MS SQL 2008 Available – DiscountASP.NET
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 6 October
MembersPro PayPal - ASP Membership software

USA & Europe Data Centers – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Option Explicit

When writing your ASP pages, there is one very important step many developers unfortunately forget to do or in some cases are too lazy to do. Always put the effort in and avoid writing sloppy code, use Option Explicit.

Option Explicit requires that all variable names be declared (with the Dim statement). Using Option Explicit to force variable declaration is good for a number of reasons. First, let's look at this code example:

<%
MyName = "michael"
Response.Write(MyNam)
%>

As I have mispelt MyName and written MyNam nothing will be printed out, ASP will create a new variable MyNam and assign an empty string to it. If your code is long then finding this typing mistake can take you hours.Trust me!!

<%
Option Explicit 'Always, always, always!
'declare your variables
Dim MyName
MyNam = "Michael"
Response.Write(MyName)
%>

Note that If you write the code in the style above using Option Explicit you will get an ASP error informing you that MyNam is undefined. You must first use the Dim statement. Debugging becomes alot easier!

Below is a section of code with an example of a spelling mistake. We have declared our variables but unfortunately mispelled one of our variables ie MyNam. Using Option Explicit the undefined variable will be picked up and the error message 'Variable is undefined' will be displayed. This can truely save you an awful lot of time when debugging your code.

Just note that the Option Explicit must come before any HTML code.

<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Using Option Explicit</title>
</head>

<body>

<%
'declare your variables
dim MyName, MyAddress, MyfootballTeam
MyNam = "michael"
Response.Write(MyName)
MyAddress = "16 Marki Street"
Response.Write(MyAddress)
MyfootballTeam ="Spurs"
Response.Write(MyfootballTeam)
%>

</body>
</html>

The above code will produce this error message.

Option Explicit

To sum up, using Option Explicit forces you to declare all your variables before you use them. If you use a variable you haven't declared then an error will be generated much like the one in the example above.

Read our related tutorial on 'Variables'

LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software