O'Reilly logo

Programming Visual Basic 2005 by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required