Option Explicit Statement

   
Option Explicit Statement

Syntax

 Option Explicit [On  Off] 

Description

Use Option Explicit to generate a compile-time error whenever a variable that has not been declared is encountered .

Rules at a Glance

  • The Option Explicit statement must appear in the declarations section of a module before any procedures.

  • In modules where the Option Explicit statement is not used, any undeclared variables are automatically cast as Objects.

  • The default is Option Explicit On . In other words, the statement:

     Option Explicit 

    is equivalent to:

     Option Explicit On 

Programming Tips and Gotchas

  • It is considered good programming practice to always use the Option Explicit statement. The following example shows why:

     1:    Dim   iVariable   As Integer 2:    iVariable = 100 3:    iVariable = iVarable + 50 4:    MsgBox iVariable 

    In this code snippet, an integer variable, iVariable , has been declared. However, because the name of the variable has been mistyped in line 3, the message box shows its value as only 50 instead of 150. This is because iVarable is assumed to be an undeclared variable whose value is 0. If the Option Explicit statement had been used, the code would not have compiled, and iVarable would have been highlighted as the cause.

  • For an ASP.NET page, you use the @ PAGE directive rather than Option Explicit to require variable declaration. Its syntax is:

     <%@ Page Language="VB" Explicit=truefalse %> 

    By default, Explicit is true in ASP.NET pages.

    You can also use the <system.web> section of the WEB.Config file to require variable declaration for an entire virtual directory or ASP.NET application by adding an explicit attribute to the compliation section. Its syntax is:

     <compliation strict="truefalse"> 

    In both cases, true corresponds to Option Explicit On , and false corresponds to Option Explicit Off .

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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