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 INFORMATIONOption 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. |
Byte | Byte, Short, Integer, Long, Decimal, Single, Double | Short | Short, Integer, Long, Decimal, Single, Double | Integer | Integer, Long, Decimal, Single, Double | Long | Long, Decimal, Single, Double | Decimal | Decimal, Single, Double | Single | Single, Double | Double | Double | Any enumerated type | Its underlying integer type and
any type to which it will widen | Char | Char, String | Any type | Object, any interface that it implements | Any derived type | Any base type from which it is
derived | Nothing | Any 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.
|
CBool | Boolean | Any numeric type (including Byte and enumerated types), String, Object | CByte | Byte | Any numeric type, any enumerated type, Boolean, String, Object | CChar | Char | String, Object | CDate | Date | String, Object | CDbl | Double | Any numeric type (including Byte and enumerated types), Boolean, String, Object | CDec | Decimal | Any numeric type (including Byte and enumerated types), Boolean, String, Object | CInt | Integer | Any numeric type (including Byte and enumerated types), Boolean, String, Object | CLng | Long | Any numeric type (including Byte and enumerated types), Boolean, String, Object | CObj | Object | Any type | CShort | Short | Any numeric type (including Byte and enumerated types), Boolean, String, Object | CSng | Single | Any numeric type (including Byte and enumerated types), Boolean, String, Object | CStr | String | Any numeric type (including Byte), Boolean, Char, Char array, Date, Object | CType | Type 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:
- 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
- Open a project template in Notepad.
- 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"
>
- Repeat steps 2 and 3 for each project template that you
want to change the default behavior for.
Modification Type: | Major | Last Reviewed: | 1/27/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbBug kbCompiler kbDebug kbIDEProject kbinfo kbUpgrade KB311329 |
---|
|