Option Strict Statement

   
Option Strict Statement

Syntax

 Option Strict [On  Off] 

Description

Option Strict prevents VB from making any implicit data type conversions that are narrowing since narrowing conversions may involve data loss. For example:

 Dim lNum As Long = 2455622 Dim iNum As Integer = lNum 

converts a Long (whose value can range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) to an Integer (whose value can range from 2,147,483,648 to 2,147,483,647). In this case, even though no data loss would result from the narrowing, Option Strict On would still not allow the conversion and would instead generate a compiler error. The reasoning here is that, although particular narrowing operations may not lose data, there is always the potential for data loss when working with variables that is, with symbolic representations of numbers whose values are allowed to vary.

Rules at a Glance

  • If the Option Strict statement is not present in a module, Option Strict is Off .

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

     Option Strict On 

    is equivalent to the statement:

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

  • Option Strict On disallows all implicit narrowing conversions.

  • Option Strict On also causes errors to be generated for late binding, as well as for any undeclared variables, since Option Strict On implies Option Explicit On .

  • Conversions can be narrowing or widening. The widening conversions are conversions from a type to itself or any of the following:

    • Byte figs/u2192.gif Short, Integer, Long, Decimal, Single, Double

    • Short figs/u2192.gif Integer, Long, Decimal, Single, Double

    • Integer figs/u2192.gif Long, Decimal, Single, Double

    • Long figs/u2192.gif Decimal, Single, Double

    • Decimal figs/u2192.gif Single, Double

    • Single figs/u2192.gif Double

    • Any enumerated type figs/u2192.gif Integer type or wider

    • Char figs/u2192.gif String

    • Any type figs/u2192.gif Object

    • Any derived type figs/u2192.gif Any type from which it is derived

    • Any type figs/u2192.gif Any interface it implements

    • Nothing figs/u2192.gif Any type

  • Narrowing conversions are:

    • The reverse conversions of the widening conversions listed above

    • Conversions between Boolean and any numeric type

    • Any numeric type figs/u2192.gif any enumerated type

    • Conversions between a Char array and a String

    • Conversions between String and any numeric, Boolean, or Date type

Programming Tips and Gotchas

  • Although the setting of Option Strict has no effect on BCL data types, BCL data types disallow implicit narrowing conversions.

  • Explicit narrowing conversions are not affected by Option Strict . However, if data loss does occur as a result of an explicit conversion, an OverflowException exception is generated.

  • One of the most commonly overlooked narrowing conversions is the use of "wider" arguments in function, procedure, and method calls. Passing a Long to an Integer parameter, for example, is an implicit narrowing conversion that Option Strict does not allow.

  • In many cases, Option Strict On disallows seemingly "safe" conversions because it interprets literal values in unexpected ways. For example, the statement:

     Dim decNum As Decimal = 10.32 

    generates a compiler error because 10.32 is interpreted as a Double, and implicit conversions from Double to Decimal are not allowed. You can correct this compiler error with a statement like:

     Dim decNum As Decimal = 10.32D 
  • Setting Option Strict On is highly recommended.

  • For an ASP.NET page, you use the @ Page directive rather than Option Strict to control strict type checking. Its syntax is:

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

    By default, Strict is false in ASP.NET pages.

    You can also use the <system.web> section of the WEB.Config file to control strict type checking for an entire virtual directory or ASP.NET application by adding a strict attribute to the compilation section. Its syntax is:

     <compilation strict="truefalse"> 

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

VB.NET/VB 6 Differences

The Option Strict setting is new to VB.NET.

See Also

Option Explicit Statement

   


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