Поделиться через


Variable Declaration in Visual Basic

You declare a variable to specify its name and characteristics. The declaration statement for variables is the Dim Statement (Visual Basic). Its location and contents determine the variable's characteristics.

For variable naming rules and considerations, see Declared Element Names (Visual Basic).

Declaration Levels

Local and Member Variables

A local variable is one that is declared within a procedure. A member variable is a member of a Visual Basic type; it is declared at module level, inside a class, structure, or module, but not within any procedure internal to that class, structure, or module.

Shared and Instance Variables

In a class or structure, the category of a member variable depends on whether or not it is shared. If it is declared with the Shared keyword, it is a shared variable, and it exists in a single copy shared among all instances of the class or structure.

Otherwise it is an instance variable, and a separate copy of it is created for each instance of the class or structure. A given copy of an instance variable is available only to the instance for which it was created. It is independent of a copy in any other instance.

Declaring Data Type

The As clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. You can specify any of the following types for a variable:

  • An elementary data type, such as Boolean, Long, or Decimal

  • A composite data type, such as an array or structure

  • An object type, or class, defined either in your application or in another application

  • A .NET Framework class, such as Label or TextBox

  • An interface type, such as IComparable or IDisposable

You can declare several variables in one statement without having to repeat the data type. In the following statements, the variables i, j, and k are declared as type Integer, l and m as Long, and x and y as Single:

Dim i, j, k As Integer
' All three variables in the preceding statement are declared as Integer.
Dim l, m As Long, x, y As Single
' In the preceding statement, l and m are Long, x and y are Single.

For more information on data types, see Data Types in Visual Basic. For more information on objects, see Objects and Classes in Visual Basic and Programming with Components.

Local Type Inference

Type inference is used to determine the data types of local variables declared without an As clause. The compiler infers the type of the variable from the type of the initialization expression. This enables you to declare variables without explicitly stating a type. In the following example, both num1 and num2 are strongly typed as integers.

Public Sub inferenceExample()

    ' Using explicit typing.
    Dim num1 As Integer = 3

    ' Using local type inference.
    Dim num2 = 3

End Sub

If you want to use local type inference, Option Infer must be set to On. For more information, see Local Type Inference (Visual Basic) and Option Infer Statement..

Declaring Characteristics

The lifetime of a variable is the period of time during which it is available for use. In general, a variable exists as long as the element that declares it (such as a procedure or class) continues to exist. In some cases it is possible to extend a variable's lifetime. For more information, see Lifetime in Visual Basic.

The scope of a variable is the set of all code that can refer to it without qualifying its name. A variable's scope is determined by where it is declared. Code located in a given region can use the variables defined in that region without having to qualify their names. For more information, see Scope in Visual Basic.

A variable's access level is the extent of code that has permission to access it. This is determined by the access modifier (such as Public (Visual Basic) or Private (Visual Basic)) that you use in the Dim statement. For more information, see Access Levels in Visual Basic.

See Also

Tasks

How to: Create a New Variable (Visual Basic)

How to: Create a Variable that Does Not Change in Value (Visual Basic)

How to: Move Data Into and Out of a Variable (Visual Basic)

Reference

Data Type Summary (Visual Basic)

Protected (Visual Basic)

Friend (Visual Basic)

Static (Visual Basic)

Option Infer Statement

Concepts

Deciding What Type of Variable to Define (Visual Basic)

Declared Element Characteristics (Visual Basic)

Local Type Inference (Visual Basic)