O'Reilly logo

Programming Visual Basic 2008 by Tim Patrick

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

Literals are nice, and constants and enumerations are nicer, but none of them can be altered once your program starts. This tends to make your application rigid and inflexible. If all your customers are named "Fred" and they only place orders for $342.34, it probably won't be much of a limitation. But most users want more variety in their software. Variables are named containers for data, just like constants, but their contents can be modified throughout the run of an application. Also, they can contain both value types and reference types. Here's the basic syntax for defining a new variable:

Dim customerName As String

The Dim keyword—originally from the word dimension—defines a new variable; in this case, a variable named customerName with a data type of String. This named container is ready to hold any String value; assign to it string literals, other string variables, or the return value from functions that generate strings. Since it is a reference type, it can also be set to Nothing, a special Visual Basic value and keyword that means "this reference type is empty, really empty."

customerName = Nothing                      ' Nothing
customerName = "Fred"                       ' Literal
customerName = GetCustomerName(customerID)  ' Function result

All variables contain their default value until set to something else. For reference types and nullable types, the default is Nothing; for numeric values, the default is 0. Booleans default to False. You can include an initial assignment as part of the Dim statement to ...

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