Select the product you need help with
Option Explicit and Option Strict in Visual Basic .NET and in Visual BasicArticle ID: 311329 - View products that this article applies to. This article was previously published under Q311329 On This PageSUMMARY This article describes two Option statements:
MORE INFORMATIONThe Option Explicit statementBy default, the Visual Basic .NET or Visual Basic 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.The Option Strict statementBy default, the Visual Basic .NET or Visual Basic 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:
Widening ConversionsThe following table lists the standard widening conversions.Collapse this table
The following conversions may lose precision:
Widening conversions always succeed, and you can always perform widening conversions implicitly. Explicit Conversion with CastingAn 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. Collapse this table
Change the Default Project ValuesYou can change the default values of Option Explicit and Option Strict. For example, when you create a new application by using Visual Basic .NET, Visual Basic 2005, or Visual Basic 2008, 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 in Visual Basic .NET, click Options on the Tools menu to open the Options dialog box. Then, change the default values for Option Explicit and Option Strict in the VB Defaults section of Projects and Solutions. To change the default values of Option Explicit and Option Strict in Visual Studio, click Options on the Tools menu to open the Options dialog box. Then, change the default values for Option Explicit and Option Strict in the VB Defaults section of Projects and Solutions. PropertiesArticle ID: 311329 - Last Review: March 19, 2008 - Revision: 4.0 APPLIES TO
| Article Translations
|


Back to the top








