Microsoft KB Archive/311329

= Option Explicit and Option Strict in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 311329

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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.



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.

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.

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:  Locate the following project template files on your system:  EmptyProjectIPF.vbproj EmptyWebProjectIPF.vbproj WebApplication.vbproj WebControl.vbproj WebService.vbproj WindowsApplication.vbproj</li> WindowsControl.vbproj</li> WindowsService.vbproj</li></ul> </li> Open a project template in Notepad.</li>  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 = &quot;Exe&quot; StartupObject = &quot;&quot; OptionExplicit = &quot;Off&quot; OptionStrict = &quot;On&quot; >                       </li> Repeat steps 2 and 3 for each project template that you want to change the default behavior for.</li></ol>

Additional query words: casting option strict explicit

Keywords: kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbbug kbcompiler kbdebug kbideproject kbinfo kbupgrade KB311329

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.