Using Variables to Store Information


Using Variables to Store Information

A variable is a temporary storage location for data in your program. You can use one or many variables in your code, and they can contain words, numbers, dates, properties, or other values. By using variables, you can assign a short and easy-to-remember name to each piece of data you plan to work with. Variables can hold information entered by the user at run time, the result of a specific calculation, or a piece of data you want to display on your form. In short, variables are handy containers that you can use to store and track almost any type of information.

Using variables in a Visual Basic program requires some planning. Before you can use a variable, you must set aside memory in the computer for the variable's use. This process is a little like reserving a seat at a theater or a baseball game. I'll cover the process of making reservations for, or declaring, a variable in the next section.

Setting Aside Space for Variables: The Dim Statement

In Microsoft Visual Basic .NET 2003 and Visual Basic 2005, you must explicitly declare your variables before using them. This is a change from Visual Basic 6 and earlier versions of Visual Basic, where (under certain circumstances) you can declare variables implicitly—in other words, simply by using them and without a Dim statement. This practice is flexible but rather risky—it creates the potential for variable confusion and misspelled variable names, which introduces potential bugs into the code that might or might not be discovered later.

To declare a variable in Visual Basic 2005, type the variable name after the Dim statement. (Dim stands for dimension.) This declaration reserves room in memory for the variable when the program runs and lets Visual Basic know what type of data it should expect to see later. Although this declaration can be done at any place in the program code (as long as the declaration happens before the variable is used), most programmers declare variables in one place at the top of their event procedures or code modules.

For example, the following statement creates space for a variable named LastName that will hold a textual, or string, value:

Dim LastName As String

Note that in addition to identifying the variable by name, I've used the As keyword to give the variable a particular type, and I've identified the type by using the keyword String. (You'll learn about other data types later in this chapter.) A string variable contains textual information: words, letters, symbols—even numbers. I find myself using string variables a lot; they hold names, places, lines from a poem, the contents of a file, and many other “wordy” data.

Why do you need to declare variables? Visual Basic wants you to identify the name and the type of your variables in advance so that the compiler can set aside the memory the program will need to store and process the information held in the variables. Memory management might not seem like a big deal to you (after all, modern personal computers have lots of RAM and gigabytes of free hard disk space), but in some programs, memory can be consumed quickly, and it's a good practice to take memory allocation seriously even as you take your first steps as a programmer. As you'll soon see, different types of variables have different space requirements and size limitations.

NOTE
In some earlier versions of Visual Basic, specific variable types (such as String or Integer) aren't required—information is simply held by using a generic (and memory hungry) data type called Variant, which can hold data of any size or format. Variants are not supported in Visual Basic 2005. Although they are handy for beginning programmers, their design makes them slow and inefficient, and they allow variables to be converted from one type to another too easily—often causing unexpected results.

After you declare a variable, you're free to assign information to it in your code by using the assignment operator (=). For example, the following program statement assigns the last name “Jefferson” to the LastName variable:

LastName = "Jefferson"

Note that I was careful to assign a textual value to the LastName variable because its data type is String. I can also assign values with spaces, symbols, or numbers to the variable, such as

LastName = "1313 Mockingbird Lane"

but the variable is still considered a string value. The number portion could only be used in a mathematical formula if it were first converted to an integer or a floating-point value by using one of a handful of conversion functions I'll discuss later in this book.

After the LastName variable is assigned a value, it can be used in place of the name “Jefferson” in your code. For example, the assignment statement

Label1.Text = LastName

displays “Jefferson” in the first label (Label1) on your form.

NOTE
If you really want to declare variables “the old way” in Visual Basic 2005—that is, without explicitly declaring them by using the Dim statement—you can place the Option Explicit Off statement at the very top of your form's or module's program code (before any event procedures), and it will defeat the Visual Basic default requirement that variables be declared before they're used. I don't recommend this statement as a permanent addition to your code, but you might find it useful temporarily as you convert older Visual Basic programs to Visual Studio 2005.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net