Option Explicit Is On by Default


Option Explicit Is On by Default

In previous versions of Active Server Pages, Option Explicit was Off by default. In ASP.NET, on the other hand, Option Explicit is On .

When Option Explicit is On , you must declare any variable before you use it. For example, although the following script works fine in ASP Classic, it generates an error in ASP.NET:

 
 <% strMessage = "Hello, how are you?" Response.Write( strMessage ) %> 

If you execute an ASP.NET page that contains this script, you receive the error message The name 'strMessage' is not declared .

You can avoid this error in two ways. If you want to follow good programming practices and get the best performance from your ASP.NET pages, you should declare all variables before you use them like this:

 
 <% Dim strMessage As String strMessage = "Hello, how are you?" Response.Write( strMessage ) %> 

Alternatively, if you just don't feel like declaring all your variables, you can turn Option Explicit Off at either the page level or server level.

To set Option Explicit Off at the page level, add the following page directive to the top of a page:

 
 <%@ Explicit="False" %> 

You can disable Option Explicit for every ASP.NET page on your Web server within the machine.config file (located under the WINNT\Microsoft.NET\Framework\[ version ]\CONFIG directory). In the <compilation> section, set the explicit attribute to the value False .



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net