Compartilhar via


Option Strict Statement

Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.

Option Strict { On | Off }

Parts

Term

Definition

On

Optional. Enables Option Strict checking.

Off

Optional. Disables Option Strict checking.

Remarks

When Option Strict On or Option Strict appears in a file, the following conditions cause a compile-time error:

  • Implicit narrowing conversions

  • Late binding

  • Implicit typing that results in an Object type

Note

In the warning configurations that you can set on the Compile Page, Project Designer (Visual Basic), there are three settings that correspond to the three conditions that cause a compile-time error. For information about how to use these settings, see To set warning configurations in the IDE later in this topic.

The Option Strict Off statement turns off error and warning checking for all three conditions, even if the associated IDE settings specify to turn on these errors or warnings. The Option Strict On statement turns on error and warning checking for all three conditions, even if the associated IDE settings specify to turn off these errors or warnings.

If used, the Option Strict statement must appear before any other code statements in a file.

When you set Option Strict to On, Visual Basic checks that data types are specified for all programming elements. Data types can be specified explicitly, or specified by using local type inference. Specifying data types for all your programming elements is recommended, for the following reasons:

  • It enables IntelliSense support for your variables and parameters. This enables you to see their properties and other members as you type code.

  • It enables the compiler to perform type checking. Type checking helps you find statements that can fail at run time because of type conversion errors. It also identifies calls to methods on objects that do not support those methods.

  • It speeds up the execution of code. One reason for this is that if you do not specify a data type for a programming element, the Visual Basic compiler assigns it the Object type. Compiled code might have to convert back and forth between Object and other data types, which reduces performance.

Implicit Narrowing Conversion Errors

Implicit narrowing conversion errors occur when there is an implicit data type conversion that is a narrowing conversion.

Visual Basic can convert many data types to other data types. Data loss can occur when the value of one data type is converted to a data type that has less precision or a smaller capacity. A run-time error occurs if such a narrowing conversion fails. Option Strict ensures compile-time notification of these narrowing conversions so that you can avoid them. For more information, see Implicit and Explicit Conversions (Visual Basic) and Widening and Narrowing Conversions (Visual Basic).

Conversions that can cause errors include implicit conversions that occur in expressions. For more information, see the following topics:

When you concatenate strings by using the & Operator (Visual Basic), all conversions to the strings are considered to be widening. So these conversions do not generate an implicit narrowing conversion error, even if Option Strict is on.

When you call a method that has an argument that has a data type different from the corresponding parameter, a narrowing conversion causes a compile-time error if Option Strict is on. You can avoid the compile-time error by using a widening conversion or an explicit conversion.

Implicit narrowing conversion errors are suppressed at compile-time for conversions from the elements in a For Each…Next collection to the loop control variable. This occurs even if Option Strict is on. For more information, see the "Narrowing Conversions" section in For Each...Next Statement (Visual Basic).

Late Binding Errors

An object is late bound when it is assigned to a property or method of a variable that is declared to be of type Object. For more information, see Early and Late Binding (Visual Basic).

Implicit Object Type Errors

Implicit object type errors occur when an appropriate type cannot be inferred for a declared variable, so a type of Object is inferred. This primarily occurs when you use a Dim statement to declare a variable without using an As clause, and Option Infer is off. For more information, see Option Infer Statement and the Visual Basic Language Specification.

For method parameters, the As clause is optional if Option Strict is off. However, if any one parameter uses an As clause, they all must use it. If Option Strict is on, the As clause is required for every parameter definition.

If you declare a variable without using an As clause and set it to Nothing, the variable has a type of Object. No compile-time error occurs in this case when Option Strict is on and Option Infer is on. An example of this is Dim something = Nothing.

Default Data Types and Values

The following table describes the results of various combinations of specifying the data type and initializer in a Dim Statement (Visual Basic).

Data type specified?

Initializer specified?

Example

Result

No

No

Dim qty

If Option Strict is off (the default), the variable is set to Nothing.

If Option Strict is on, a compile-time error occurs.

No

Yes

Dim qty = 5

If Option Infer is on (the default), the variable takes the data type of the initializer. See Local Type Inference (Visual Basic).

If Option Infer is off and Option Strict is off, the variable takes the data type of Object.

If Option Infer is off and Option Strict is on, a compile-time error occurs.

Yes

No

Dim qty As Integer

The variable is initialized to the default value for the data type. For more information, see Dim Statement (Visual Basic).

Yes

Yes

Dim qty As Integer = 5

If the data type of the initializer is not convertible to the specified data type, a compile-time error occurs.

When an Option Strict Statement Is Not Present

If the source code does not contain an Option Strict statement, the Option strict setting on the Compile Page, Project Designer (Visual Basic) is used. The Compile Page has settings that provide additional control over the conditions that generate an error.

If you are using the command-line compiler, you can use the /optionstrict compiler option to specify a setting for Option Strict.

To set Option Strict in the IDE

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings.

  1. In Solution Explorer, select a project. On the Project menu, click Properties. For more information, see Introduction to the Project Designer.

  2. On the Compile tab, set the value in the Option Strict box.

To set warning configurations in the IDE

When you use the Compile Page, Project Designer (Visual Basic) instead of an Option Strict statement, you have additional control over the conditions that generate errors. The Warning configurations section of the Compile Page has settings that correspond to the three conditions that cause a compile-time error when Option Strict is on. Following are these settings:

  • Implicit conversion

  • Late binding; call could fail at run time

  • Implicit type; object assumed

When you set Option Strict to On, all three of these warning configuration settings are set to Error. When you set Option Strict to Off, all three settings are set to None.

You can individually change each warning configuration setting to None, Warning, or Error. If all three warning configuration settings are set to Error, On appears in the Option strict box. If all three are set to None, Off appears in this box. For any other combination of these settings, (custom) appears.

To set the Option Strict default setting for new projects

When you create a project, the Option Strict setting on the Compile tab is set to the Option Strict setting in the Options dialog box.

To set Option Strict in this dialog box, on the Tools menu, click Options. In the Options dialog box, expand Projects and Solutions, and then click VB Defaults. The initial default setting in VB Defaults is Off.

To set Option Strict on the command line

Include the /optionstrict compiler option in the vbc command.

Example

The following examples demonstrate compile-time errors caused by implicit type conversions that are narrowing conversions. This category of errors corresponds to the Implicit conversion condition on the Compile Page.

' If Option Strict is on, this implicit narrowing 
' conversion causes a compile-time error. 
' The commented statements below use explicit 
' conversions to avoid a compile-time error. 
Dim cyclists As Long = 5
Dim bicycles As Integer = cyclists
'Dim bicycles As Integer = CType(cyclists, Integer) 
'Dim bicycles As Integer = CInt(cyclists) 
'Dim bicycles As Integer = Convert.ToInt32(cyclists) 


' If Option Strict is on, this implicit narrowing 
' conversion causes a compile-time error. 
' The commented statements below use explicit 
' conversions to avoid a compile-time error. 
Dim charVal As Char = "a" 
'Dim charVal As Char = "a"c 
'Dim charVal As Char = CType("a", Char) 


' If Option Strict is on, a compile-time error occurs. 
' If Option Strict is off, the string is implicitly converted 
' to a Double, and then is added to the other number. 
Dim myAge As Integer = "34" + 6


' If Option Strict is on, a compile-time error occurs. 
' If Option Strict is off, the floating-point number 
' is implicitly converted to a Long. 
Dim num = 123.45 \ 10

The following example demonstrates a compile-time error caused by late binding. This category of errors corresponds to the Late binding; call could fail at run time condition on the Compile Page.

' If Option Strict is on, this late binding 
' causes a compile-time error. If Option Strict 
' is off, the late binding instead causes a 
' run-time error. 
Dim punchCard As New Object
punchCard.Column = 5

The following examples demonstrate errors caused by variables that are declared with an implicit type of Object. This category of errors corresponds to the Implicit type; object assumed condition on the Compile Page.

' If Option Strict is on and Option Infer is off, 
' this Dim statement without an As clause  
' causes a compile-time error. 
Dim cardReaders = 5

' If Option Strict is on, a compile-time error occurs. 
' If Option Strict is off, the variable is set to Nothing. 
Dim dryWall
' If Option Strict is on, this parameter without an 
' As clause causes a compile-time error. 
Private Sub DetectIntergalacticRange(ByVal photonAttenuation)

End Sub

See Also

Tasks

How to: Access Members of an Object (Visual Basic)

Reference

Compile Page, Project Designer (Visual Basic)

Option Explicit Statement (Visual Basic)

Type Conversion Functions (Visual Basic)

/optionstrict

Visual Basic Defaults, Projects, Options Dialog Box

Concepts

Widening and Narrowing Conversions (Visual Basic)

Implicit and Explicit Conversions (Visual Basic)

Embedded Expressions in XML (Visual Basic)

Relaxed Delegate Conversion (Visual Basic)

Late Binding in Office Solutions