Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual Basic
F-P Statements
 Option Strict Statement
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Basic Language Reference
Option Strict Statement

Updated: July 2008

Restricts implicit data type conversions to only widening conversions.

Option Strict { On | Off }
On

Optional. Enables Option Strict checking.

Off

Optional. Disables Option Strict checking. If On or Off is not specified, the default is Off.

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

Visual Basic allows conversions of many data types to other data types. Data loss can occur when the value of one data type is converted to a data type with less precision or 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 they can be avoided.

Note:

The narrowing-conversion error is suppressed for conversions from the elements in a For Each…Next collection to the loop control variable. For more information and examples, see the "Narrowing Conversions" section in For Each...Next Statement (Visual Basic).

In addition to disallowing implicit narrowing conversions, Option Strict generates an error for late binding. An object is late bound when it is assigned to a variable that is declared to be of type Object.

Because Option Strict On provides strong typing, prevents unintended type conversions with data loss, disallows late binding, and improves performance, its use is strongly recommended.

Note:

The compiler default is Option Strict Off if you do not specify Option Strict in your code.

You can also set Option Strict in the Visual Studio integrated development environment (IDE) or on a command line.

Note:

The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, click Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To set Option Strict in the IDE

  1. On the Tools menu, click Options.

  2. Open the Projects and Solutions node.

  3. Choose VB Defaults.

  4. Modify the Option Strict setting.

To set Option Strict on the command line

The following example demonstrates how the Option Strict statement disallows late binding and conversions when data would be lost.

Visual Basic
Option Strict On

Visual Basic
Dim thisVar As Integer
Dim thisObj As Object = New widget
thisVar = 1000   ' Declared variable does not generate error.
' Attempting to convert Double to Integer generates a COMPILER ERROR.
thisVar = 1234567890.9876542 ' causes ERROR
' Late-bound call generates a COMPILER ERROR.
Call thisObj.Method1() ' causes ERROR

Date

History

Reason

July 2008

Added a note about narrowing conversions and For Each…Next.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker