Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))

Using Variables in a Program

Variables can maintain the same value throughout a program, or they can change values several times, depending on your needs. The following exercise demonstrates how a variable named LastName can contain different text values and how the variable can be assigned to object properties.

Change the value of a variable

  1. Start Visual Studio.

  2. On the File menu, click Open Project.

    The Open Project dialog box appears.

  3. Open the Variable Test project in the c:\vb05sbs\chap05\variable test folder.

  4. If the project's form isn't visible, click Form1.vb in Solution Explorer, and then click the View Designer button.

    The Variable Test form appears in the Designer. Variable Test is a skeleton program—it contains a form with labels and buttons for displaying output, but little program code. (I create these skeleton programs now and then to save you time, although you can also create the project from scratch.) You'll add code in this exercise.

    The Variable Test form looks like this:

    The form contains two labels and two buttons. You'll use variables to display information in each of the labels.

    NOTE

    The label objects look like boxes because I set their BorderStyle properties to Fixed3D.

  5. Double-click the Show button.

    The Button1_Click event procedure appears in the Code Editor.

  6. Type the following program statements to declare and use the LastName variable:

    Dim LastName As String LastName = "Luther" Label1.Text = LastName LastName = "Bodenstein von Karlstadt" Label2.Text = LastName

    The program statements are arranged in three groups. The first statement declares the LastName variable by using the Dim statement and the String type. After you type this line, Visual Studio places a jagged line under the LastName variable, because it has been declared but not used in the program. There is nothing wrong here—Visual Studio is just reminding you that a new variable has been created and is waiting to be used.

    TIP

    If you finish writing your program and the variable name is still underlined, it could be a sign that you misspelled a variable name somewhere within your code.

    The second and third lines assign the name “Luther” to the LastName variable and then display this name in the first label on the form. This example demonstrates one of the most common uses of variables in a program—transferring information to a property. As you have seen before, all string values assigned to variables are displayed in red type.

    The fourth line assigns the name “Bodenstein von Karlstadt” to the LastName variable (in other words, it changes the contents of the variable). Notice that the second string is longer than the first and contains a few blank spaces. When you assign text strings to variables, or use them in other places, you need to enclose the text within quotation marks. (You don't need to do this with numbers.)

    Finally, keep in mind another important characteristic of the variables being declared in this event procedure—they maintain their scope, or hold their value, only within the event procedure you're using them in. Later in this chapter, you'll learn how to declare variables so that they can be used in any of your form's event procedures.

  7. Click the Form1.vb [Design] tab to display the form again.

  8. Double-click the Quit button.

    The Button2_Click event procedure appears in the Code Editor.

  9. Type the following program statement to stop the program:

    End

    Your screen looks like this:

  10. Click the Save All button on the Standard toolbar to save your changes.

  11. Click the Start Debugging button on the Standard toolbar to run the program.

    The program runs in the IDE.

  12. Click the Show button.

    The program declares the variable, assigns two values to it, and copies each value to the appropriate label on the form. The program produces the output shown below.

  13. Click the Quit button to stop the program.

    The program stops, and the development environment returns.

Variable Naming Conventions

Naming variables can be a little tricky because you need to use names that are short but intuitive and easy to remember. To avoid confusion, use the following conventions when naming variables:

Категории