Type Checking in Visual Basic
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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.

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

© 2016 Microsoft