Type Checking in Visual Basic

When converting between data types, the Visual Basic compiler can operate under strict or permissive type semantics. If strict type semantics are in effect, only widening conversions are permitted implicitly, and narrowing conversions must be explicit. Under permissive type semantics, you can attempt all widening and narrowing conversions implicitly. Type semantics apply to conversions between all data types, including object types.

Ways to Set the Type Checking Option

Visual Basic provides a switch that controls type checking. By default, this switch is Off, and the compiler allows implicit narrowing conversions. If you turn the switch On, the compiler enforces strict type semantics.

Type Checking Switch

You can set the type checking switch On or Off in any of the following ways:

  • Set the appropriate project property in the Properties window of the integrated development environment (IDE)

  • Specify the /optionstrict command-line compiler option

  • Include the Option Strict statement at the beginning of your code

If you use the OptionStrict statement, that setting overrides both the project property and compiler option settings, but only for the source code file in which it appears. For more information, see Option Strict Statement.

Performance Advantage

Setting Option Strict to On has the advantage of forcing early binding. This improves performance.

See Also

Tasks

How to: Modify Project Properties and Configuration Settings

Concepts

Data Types in Visual Basic

Typeless Programming in Visual Basic

Implicit and Explicit Declaration

Object as the Universal Data Type

Efficient Use of Data Types

Widening and Narrowing Conversions

Implicit and Explicit Conversions

Reference

Data Type Summary (Visual Basic)

Type Conversion Functions