Option Explicit and Option Strict in Visual Basic .NET and in Visual Basic

Article translations Article translations
Article ID: 311329 - View products that this article applies to.
This article was previously published under Q311329
Expand all | Collapse all

On This Page

SUMMARY

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

MORE INFORMATION

The Option Explicit statement

By 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 statement

By 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:
  • 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.

Collapse this tableExpand this table
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.
Collapse this tableExpand this table
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. 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.

Properties

Article ID: 311329 - Last Review: March 19, 2008 - Revision: 4.0
APPLIES TO
  • Microsoft Visual Studio 2008 Academic Edition
  • Microsoft Visual Studio 2008 Professional Edition
  • Microsoft Visual Studio 2008 Standard Edition
  • Microsoft Visual Studio Team System 2008 Database Edition
  • Microsoft Visual Studio Team System 2008 Architecture Edition
  • Microsoft Visual Studio Team System 2008 Development Edition
  • Microsoft Visual Studio Team System 2008 Test Edition
  • Microsoft Visual Studio Team System 2008 Team Foundation Server
  • Microsoft Visual Studio Team System 2008 Team Suite
  • Microsoft Visual Studio Team System 2008 Test Load Agent
  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbbug kbcompiler kbdebug kbideproject kbinfo kbupgrade KB311329

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com