Variables

A variable is an identifier that is declared in a method and that stands for a value within that method. Its value is allowed to change within the method. Each variable is of a particular type, and that type is indicated in the declaration of the variable. For example, this line declares a variable named i whose type is Integer:

Dim i As Integer

The keyword Dim indicates a variable declaration. Dim is short for dimension and dates back to the original days of the BASIC programming language in the late 1960s. In that language, variables were not declared; they were just used where needed (except for arrays). Because of how arrays were laid out in memory, the BASIC language interpreter had to be told of the dimensions of an array before the array was used. This was the purpose of the Dim statement. In later years, when declaration of all variables was agreed upon to be a good thing, the use of the Dim statement was broadened to include all variable declarations.

Variable identifiers may be suffixed with type characters that serve to indicate the variable’s type. For example, this line declares a variable of type Integer:

Dim x%

The effect is precisely the same as for this declaration:

Dim x As Integer

The set of type characters is shown in Table 2-4; note that not all data types have a type character.

Table 2-4. Type characters

Data type

Type character

Example

Decimal

@

Dim decValue@ = 132.24

Double

#

Dim dblValue# = .0000001327

Integer

%

Dim iCount% = 100 ...

Get Programming Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.