Name

Option Strict Statement

Synopsis


Option Strict [On | Off]

Description

Option Strict prevents VB from making any implicit narrowing data type conversions, since they may involve data loss. It also causes errors to be generated for late binding, as well as for any undeclared variables. Option Strict On implies Option Explicit On. For example:

Dim lNum As Long = 2455622
Dim iNum As Integer = lNum

converts a Long (whose value can range from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) to an Integer (whose value can range from 2,147,483,648 to 2,147,483,647). In this case, even though no data loss would result from the narrowing, Option Strict On would still not allow the conversion and would instead generate a compiler error.

If the Option Strict statement is not present in a module, Option Strict is Off. The default is Option Strict On. In other words, the statement:

Option Strict On

is equivalent to the statement:

Option Strict

The Option Strict statement must appear in the declarations section of a module before any code.

The VB compiler considers the following to be narrowing conversions:

  • Short, Integer, Long, Decimal, Single, DoubleByte

  • Integer, Long, Decimal, Single, Double Short

  • Long, Decimal, Single, Double Integer

  • Decimal, Single, DoubleLong

  • Single, DoubleDecimal

  • DoubleSingle

  • Integer type or wider Any enumerated type

  • StringChar

  • ObjectAny type

  • Any base typeAny derived type

  • An interfaceAny type implementing the interface

  • Any typeNothing

  • Conversions between Boolean ...

Get VB.NET Language Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.