Option Strict Statement |
Option Strict [On Off]
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.
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 Short, Integer, Long, Decimal, Single, Double
Short Integer, Long, Decimal, Single, Double
Integer Long, Decimal, Single, Double
Long Decimal, Single, Double
Decimal Single, Double
Single Double
Any enumerated type Integer type or wider
Char String
Any type Object
Any derived type Any type from which it is derived
Any type Any interface it implements
Nothing Any type
Narrowing conversions are:
The reverse conversions of the widening conversions listed above
Conversions between Boolean and any numeric type
Any numeric type any enumerated type
Conversions between a Char array and a String
Conversions between String and any numeric, Boolean, or Date type
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 .
The Option Strict setting is new to VB.NET.
Option Explicit Statement