Export (0) Print
Expand All

6.2.2 Option Strict Statement

Visual Studio .NET 2003

The Option Strict statement determines whether conversions and operations on Object are governed by strict or permissive type semantics and whether types are implicitly typed as Object if no As clause is specified. The statement may be followed by the keywords On or Off; if neither is specified, the default is On. If no statement is specified in a file, the compilation environment determines which will be used.

Note   Strict and Off are not reserved words.
Option Strict On

Module Test
    Sub Main()
        Dim x ' Error, no type specified.
        Dim o As Object
        Dim b As Byte = o ' Error, narrowing conversion.

        o.Foo() ' Error, late binding disallowed.
        o = o + 1 ' Error, addition is not defined on Object.
    End Sub
End Module

Under strict semantics, the following are disallowed:

  • Narrowing conversions without an explicit cast operator.
  • Late binding.
  • Operations on type Object other than =, <>, TypeOf...Is, and Is.
  • Omitting the As clause in a declaration.
OptionStrictStatement ::= Option Strict [ OnOff ] LineTerminator

See Also

6.2.1 Option Explicit Statement | 6.2.3 Option Compare Statement | 6.2.4 Integer Overflow Checks | 6.2 Compilation Options | Option Strict Statement (Visual Basic Language Reference)

© 2015 Microsoft