Using Variables in a Program


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:

    graphic

    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:

    graphic

  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.

    graphic

  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:

  • Begin each variable name with a letter or underscore. This is a Visual Basic requirement. Variable names can contain only letters, underscores, and numbers.

  • Although variable names can be virtually any length, try to keep them under 33 characters to make them easier to read. (Variable names are limited to 255 characters in Visual Basic 6, but that's no longer a constraint.)

  • Make your variable names descriptive by combining one or more words when it makes sense to do so. For example, the variable name SalesTaxRate is much clearer than Tax or Rate.

  • Use a combination of uppercase and lowercase characters and numbers. An accepted convention is to capitalize the first letter of each word in a variable; for example, DateOfBirth. However, some programmers prefer to use so-called camel casing (making the first letter of a variable name lowercase) to distinguish variable names from functions and module names, which usually begin with uppercase letters. Examples of camel casing include dateOfBirth, employeeName, and counter.

  • Don't use Visual Basic keywords, objects, or properties as variable names. If you do, you'll get an error when you try to run your program.

  • Optionally, you can begin each variable name with a two-character or three- character abbreviation corresponding to the type of data that's stored in the variable. For example, use strName to show that the Name variable contains string data. Although you don't need to worry too much about this detail now, you should make a note of this convention for later—you'll see it in the Visual Studio online Help and in many of the advanced books about Visual Basic programming. (This convention and abbreviation scheme was originally created by Microsoft Distinguished Engineer Charles Simonyi and is sometimes called the Hungarian Naming Convention.)



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