ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
An Overview of ASP.NET
Connections, Commands, And Procedures
What is ASP?

QUICK TIP:
Reference Recordset Fields by Any Name
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Quick Tips


Use <% OPTION EXPLICIT %>
By default, Microsoft's VBScript lets you implicitly declare variables in your code. For example, suppose you've written a function that returns the square root of a number:

Function MyFunction (num)
NewNumber = abs(num)
MyFunction = sqr(NewNumber)
End Function


VBScript automatically creates the variable NewNumber without you explicitly having to declare it before you use it. This feature can cause unusual results if you accidentally misspell the variable, as shown here:

Function MyFunction (num)
NewNumber = abs(num)
MyFunction = sqr(NewNumer)
' NewNumer rather than NewNumber
End Function


The misspelling above causes VBScript to create two variables, one called NewNumber and another one called NewNumer. The presence of the two variables will return an incorrect value to the calling function instead of the intended square root.

To avoid this problem, use the <% OPTION EXPLICIT %> function to require that all variables be declared first, which lets the VBScript interpreter catch errors such as a misspelled variable. You simple place the <% OPTION EXPLICIT %> tag at the beginning of your ASP file:

<%@ LANGUAGE=VBScript %>
<% OPTION EXPLICIT %>
<HTML>
<HEAD>
<META name="generator" content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
Function MyFunction (num)
Dim NewNumber
NewNumber = abs(num)
MyFunction = sqr(NewNumber)
End Function
%>
<P>The Square root of 64 is <% =MyFunction(64) %></P>

</BODY>
</HTML>


Because the example forces <% OPTION EXPLICIT %>, you must declare the NewNumber variable. That way, the interpreter catches any misspellings by raising an error message if it finds a variable that hasn't been declared.
Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to: webmaster@asp101.com.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers