Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 10 Creating Modules and Procedures 255



  1. 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 name by selecting the project in Solution Explorer, clicking
the Track Wins Properties command on the Project menu, and then examining or
changing the text in the Root Namespace text box on the Application tab.

Creating Procedures


Procedures provide a way to group a set of related statements to perform a task. Visual Basic
includes two primary types of procedures:

n Function procedures are called by name from event procedures or other procedures.
Often used for calculations, function procedures can receive arguments and always
return a value in the function name.
n Sub procedures are called by name from event procedures or other procedures. They
can receive arguments and also pass back modified values in an argument list. Unlike
Free download pdf