Variables

A variable is an object that holds a value:

Dim myVariable as Integer = 15

In this example, myVariable is an object of type Integer. It has been initialized with the value 15. The formal definition for how you create a variable is:

               Access-Modifier Identifier As Type [= value]

Access modifiers are discussed later in this chapter; for now, you'll use Dim.

Tip

The keyword Dim is short for dimension. This term dates back to the early days of BASIC programming, and is essentially vestigial.

An identifier is just a name for a variable, method, class, and so forth. In the case shown previously, the variable's identifier is myVariable. The keyword As signals that what follows is the type, in this case Integer. If you are initializing the variable, you follow the type with the assignment operators (=) followed by the value (e.g., 15)

Type characters

Variable identifiers may have a type character suffix that indicates the variable's type. For example, rather than writing as Integer you can use the suffix %.

Dim myVariable as Integer
Dim myVariable%

These two lines are identical in effect. Not every type is a character, but you are free to use them for those types that do. The complete set is shown in Table 16-2.

Table 16-2. Type characters

Type

Type character

Usage

Decimal

@

Dim decimalValue@ = 123.45

Double

#

Dim doubleValue# = 3.14159265358979

Integer

%

Dim integerValue% = 1

Long

&

Dim longValue& = 123456789

Single

!

Dim singleValue! = 3.1415 ...

Get Programming Visual Basic 2005 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.