Option Explicit and Option Strict in Visual Basic .NET or in Visual Basic 2005 (311329)



The information in this article applies to:

  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q311329

SUMMARY

This article describes two Option statements:
  • Option Strict
  • Option Explicit
Option Strict is new in Visual Basic .NET or Visual Basic 2005, and Option Explicit is available in previous versions of Microsoft Visual Basic.

MORE INFORMATION

Option Explicit Statement

By default, the Visual Basic .NET or Visual Basic 2005 compiler enforces explicit variable declaration, which requires that you declare every variable before you use it. To change this default behavior, see the Change the Default Project Values section.

Option Strict Statement

By default, the Visual Basic .NET or Visual Basic 2005 compiler does not enforce strict data typing. To change this default behavior, see the Change the Default Project Values section.

Option Strict restricts implicit data type conversions to only widening conversions. Widening conversions explicitly do not permit any data type conversions in which data loss may occur and any conversion between numeric types and strings. For more information about widening conversions, see the Widening Conversions section.

When you use the Option Strict statement, the statement must appear before any other code. In Visual Basic .NET, you can typically convert any data type to any other data type implicitly. Data loss can occur when the value of one data type is converted to a data type with less precision or with a smaller capacity. However, you receive a run-time error message if data will be lost in such a conversion. Option Strict notifies you of these types of conversions at compile time so that you can avoid them.

Option Strict also generates an error message in the following scenarios:
  • For any undeclared variable. This is because Option Strict also implies Option Explicit.
  • Late binding.

Widening Conversions

The following table lists the standard widening conversions.

Data TypeWidens to Data Types
ByteByte, Short, Integer, Long, Decimal, Single, Double
ShortShort, Integer, Long, Decimal, Single, Double
IntegerInteger, Long, Decimal, Single, Double
LongLong, Decimal, Single, Double
DecimalDecimal, Single, Double
SingleSingle, Double
DoubleDouble
Any enumerated typeIts underlying integer type and any type to which it will widen
CharChar, String
Any typeObject, any interface that it implements
Any derived typeAny base type from which it is derived
NothingAny data type or object type

The following conversions may lose precision:
  • Integer to Single
  • Long to Single or Double
  • Decimal to Single or Double
However, these conversions do not lose information or magnitude.

Widening conversions always succeed, and you can always perform widening conversions implicitly.

Explicit Conversion with Casting

An explicit conversion uses a type conversion keyword. Visual Basic .NET or Visual Basic 2005 provides several such keywords, which coerce an expression in parentheses to the data type that you want. These keywords behave as functions, but the compiler generates the code inline. Therefore, execution is a little faster with explicit conversion than with a function call.

The following table lists the available conversion keywords.
Type Conversion KeywordConverts Expression
to Data Type
Permitted Data Types of Expression to Be Converted
CBoolBooleanAny numeric type (including Byte and enumerated types), String, Object
CByteByteAny numeric type, any enumerated type, Boolean, String, Object
CCharCharString, Object
CDateDateString, Object
CDblDoubleAny numeric type (including Byte and enumerated types), Boolean, String, Object
CDecDecimalAny numeric type (including Byte and enumerated types), Boolean, String, Object
CIntIntegerAny numeric type (including Byte and enumerated types), Boolean, String, Object
CLngLongAny numeric type (including Byte and enumerated types), Boolean, String, Object
CObjObjectAny type
CShortShortAny numeric type (including Byte and enumerated types), Boolean, String, Object
CSngSingleAny numeric type (including Byte and enumerated types), Boolean, String, Object
CStrStringAny numeric type (including Byte), Boolean, Char, Char array, Date, Object
CTypeType specified following the comma (,)When you convert to an elementary type (including an array of an elementary type), the same types as are permitted for the corresponding conversion keyword.

When you convert to a composite type, the interfaces it implements and the classes from which it inherits.

Change the Default Project Values

You can change the default values of Option Explicit and Option Strict on a per project type basis. For example, when you create a new Visual Basic .NET or Visual Basic 2005 application, the value for Option Explicit is set to On. You can change this default value to Off.

To change the default values of Option Explicit and Option Strict, follow these steps:
  1. Locate the following project template files on your system:
    • EmptyProjectIPF.vbproj
    • EmptyWebProjectIPF.vbproj
    • WebApplication.vbproj
    • WebControl.vbproj
    • WebService.vbproj
    • WindowsApplication.vbproj
    • WindowsControl.vbproj
    • WindowsService.vbproj
  2. Open a project template in Notepad.
  3. Add (or edit if they are already present) the OptionStrict and OptionExplicit lines in the <Settings> section of the template.

    For example, the following code demonstrates how to set OptionExplicit to Off and OptionStrict to On:
    <VisualStudioProject>
        <VisualBasic>
            <Build>
                <Settings 
                    OutputType = "Exe" 
                    StartupObject = ""
                    OptionExplicit = "Off"
                    OptionStrict = "On"
                >
    						
  4. Repeat steps 2 and 3 for each project template that you want to change the default behavior for.

Modification Type:MajorLast Reviewed:1/27/2006
Keywords:kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbBug kbCompiler kbDebug kbIDEProject kbinfo kbUpgrade KB311329