Working with Public Variables


Working with Public Variables

Declaring a global, or public, variable in a module is simple—you type the keyword Public followed by the variable name and a type declaration. After you declare the variable, you can read it, change it, or display it in any procedure in your program. For example, the program statement

Public RunningTotal As Integer

declares a public variable named RunningTotal of type Integer.

The following exercises demonstrate how you can use a public variable named Wins in a module. You'll revisit Lucky Seven, the first program you wrote in this book, and you'll use the Wins variable to record how many spins you win as the slot machine runs.

NOTE
Lucky Seven is the slot machine program from Chapter 2, “Writing Your First Program.”

Revisit the Lucky Seven project

  1. Click the Close Project command on the File menu to close the Module Test project.

    Because you haven't saved the project yet, you see the following dialog box:

    graphic

    You don't need to keep this project on your hard disk; it was only for testing purposes. To demonstrate the new “close without saving” feature in Visual Studio 2005, you'll discard the project now.

  2. Click the Discard button.

    Visual Studio discards the entire project, removing any temporary files associated with the module from your computer's memory and hard disk. It seems like a rather obvious feature, but I wanted to demonstrate that the ability to close a project without saving it is a welcome improvement to the software and just the thing for this type of test. Now you'll open a more substantial project and modify it.

  3. Open the TrackWins project in the c:\vb05sbs\chap10\trackwins\lucky7 folder.

    The project opens in the IDE.

  4. If the form isn't visible, display it now.

    You see the following user interface:

    graphic

    The Track Wins project is the same slot machine program that you created in Chapter 2. With this program, the user can click a spin button to display “random” numbers in three number boxes, and if the number 7 appears in one of the boxes, the computer beeps and displays a bitmap showing an enticing, though quite dated, cash payout. I've simply renamed the Lucky7 solution in this chapter so that you won't confuse this new version with the original.

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

  6. Click the Spin button six or seven times, and then click the End button.

    As you might recall, the program uses the Rnd function to generate three random numbers each time you click the Spin button. If one of the numbers is a 7, the event procedure for the Spin button (Button1_Click) displays a cash payout picture and beeps.

Now you'll edit the form and add a module to enhance the program.

Add a module

  1. Click the Label control in the Toolbox, and then create a new rectangular label on the form below the Lucky Seven label.

  2. Set the properties shown in the following table for the new label. To help identify the new label in the program code, you'll change the new label object's name to lblWins.

    Object

    Property

    Setting

    Label5

    Font

    ForeColor

    Name

    Text

    TextAlign

    Arial, Bold Italic, 12-point

    Green (on Custom tab)

    lblWins

    “Wins: 0”

    MiddleCenter

    When you've finished, your form looks similar to this:

    graphic

    Now you'll add a new module to the project.

  3. Click the Add New Item command on the Project menu, select the Module template, and then click Add.

    A module named Module1.vb appears in the Code Editor.

  4. Move the insertion point to the blank line between the Module Module1 and End Module statements, type Public Wins As Short, and then press Enter.

    This program statement declares a public variable of the Short integer type in your program. It's identical to a normal variable declaration you might make in your program code, except the Public keyword has been substituted for the Dim keyword. When your program runs, each event procedure in the program will have access to this variable. Your module looks like this:

    graphic

  5. In Solution Explorer, click TrackWins.vb, click the View Designer button, and then double-click the Spin button.

    The Button1_Click event procedure for the Spin button appears in the Code Editor.

  6. Type the following statements below the Beep() statement in the event procedure:

    Wins = Wins + 1 lblWins.Text = "Wins: " & Wins

    This part of the program code increments the Wins public variable if a 7 appears during a spin. The second statement uses the concatenation operator (&) to assign a string to the lblWins object in the format Wins: X, in which X is the number of wins. The completed event procedure looks like this:

    graphic

  7. Click the Save All button on the Standard toolbar to save all your changes to disk.

    Save All saves your module changes as well as the changes on your form and in your event procedures.

  8. Click the Start Debugging button to run the program.

  9. Click the Spin button until you have won a few times.

    The Wins label keeps track of your jackpots. Each time you win, it increments the total by 1. After 10 spins, I had the output shown below.

    NOTE
    The exact number of wins will be different each time you run the program, due to the Randomize statement in the Form1_Load event procedure.

    graphic

  10. Click End to exit the program.

The public variable Wins was useful in the previous procedure because it maintained its value through several calls to the Button1_Click event procedure. If you had declared Wins locally in the Button1_Click event procedure, the variable would have reset each time, just as the trip odometer in your car does when you reset it. By using a public variable in a module, you can avoid “hitting the reset button.”

Public Variables vs. Form Variables

In the preceding exercise, you used a public variable to track the number of wins in the slot machine program. Alternatively, you could have declared the Wins variable at the top of the form's program code. Both techniques produce the same result because both a public variable and a variable declared in the general declarations area of a form have scope throughout the entire form. Public variables are unique, however, because they maintain their values in all the forms and modules you use in a project—in other words, in all the components that share the same project namespace. The project namespace keyword is set automatically when you first save your project. You can view or change the namespace text by selecting the project in Solution Explorer, clicking the TrackWins Properties command on the Project menu, and then examining or changing the text in the Root Namespace text box.



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