Udostępnij za pośrednictwem


Typeless Programming in Visual Basic

The Visual Basic compiler is capable of enforcing two rules that affect your source code:

  • Every local variable must appear in a declaration statement before being used in any other kind of statement. This rule is enforced by default.

  • Every narrowing type conversion must be specified with a type conversion keyword. This rule is not enforced by default.

Compiler Options that Change the Rules

You can change either or both of these rules by resetting corresponding compiler options. If you relax the type-conversion rule, the compiler operates under permissive type semantics, which allows you to make narrowing conversions without conversion keywords. For more information, see Type Checking in Visual Basic.

If you relax the variable-declaration rule, you can then use typeless programming, which permits you to refer to variables without first declaring them. For more information, see Implicit and Explicit Declaration (Visual Basic).

Typeless programming is not usually recommended, because it can lead to subtle execution errors involving data types. However, you might find it useful in some circumstances, such as porting an old application that did not declare variables.

See Also

Tasks

Troubleshooting Data Types (Visual Basic)

Reference

Option Explicit Statement (Visual Basic)

Option Strict Statement

Concepts

Data Types in Visual Basic

Type Characters (Visual Basic)

Composite Data Types (Visual Basic)

Object as the Universal Data Type (Visual Basic)

Efficient Use of Data Types (Visual Basic)

Other Resources

Elementary Data Types (Visual Basic)

Type Conversions in Visual Basic