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

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
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?

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

Quick Tips

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 %>
<META name="generator" content="Microsoft Visual Studio 6.0">
Function MyFunction (num)
Dim NewNumber
NewNumber = abs(num)
MyFunction = sqr(NewNumber)
End Function
<P>The Square root of 64 is <% =MyFunction(64) %></P>


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:

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



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