Compartilhar via


Opção declaração estrito

Restricts implicit data type conversions to only widening conversions.

Option Strict { On | Off }

Parts

Term

Definition

On

Optional. Enables Option Strict checking.

Off

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

Comentários

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. Um tempo de execução- erro ocorre se tal um Estreitando conversão falhar. Option Strictgarante a compilar-tempo notificação desses restringir conversões para que eles podem ser evitados.

ObservaçãoObservação

O erro de conversão-restrição é suprimida para conversões de elementos em um For Each…Next variávelde controle decoleção para o loop . Para obter mais informações e exemplos, consulte "Restringir conversões" seção Instrução For Each...Next (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.

ObservaçãoObservação

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.

ObservaçãoObservação

Seu computador pode mostrar nomes ou locais diferentes para alguns dos elementos da interface do usuário do Visual Studio nas instruções a seguir. A edição do Visual Studio que você possui e as configurações que você usa determinam esses elementos. Para obter mais informações, consulte Configurações do Visual Studio.

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

Exemplo

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

Option Strict On
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

Consulte também

Referência

Opção Comparar Declarações

Instrução Option Explicit (Visual Basic)

/optioncompare

/optionexplicit

/optionstrict

Caixa de diálogo Opções de padrões, projetos, Visual Basic

Conceitos

Conversões de expansão e restrição (Visual Basic)