247
Chapter 10
Creating Modules and Procedures
After completing this chapter, you will be able to:
n Employ structured programming techniques and create modules containing public
variables and procedure definitions.
n Practice using public variables that have a global scope.
n Increase programming efficiency by creating user-defined Sub and Function
procedures.
n Master the syntax for calling and using user-defined procedures.
n Pass arguments to procedures by value and by reference.
In the first nine chapters of this book, you have used event procedures such as Button1_Click,
Timer1_Tick, and Form1_Load to manage events and organize the flow of your programs.
In Microsoft Visual Basic programming, all executable statements must be placed inside
some procedure; only general declarations and instructions to the compiler can be placed
outside a procedure’s scope. In this chapter, you’ll continue to organize your programs by
breaking computing tasks into discrete logical units.
You’ll start by learning how to create modules, which are separate areas within a program
that contain global, or public, variables and Function and Sub procedures. You’ll learn how to
declare and use public variables, and you’ll learn how to build general-purpose procedures
that save coding time and can be used in more than one project. The skills you’ll learn will be
especially applicable to larger programming projects and team development efforts.
Working with Modules
As you write longer programs, you’re likely to have several forms and event procedures
that use some of the same variables and routines. By default, variables are local to an event
procedure—they can be read or changed only within the event procedure in which they
were created. You can also declare variables at the top of a form’s program code and give
the variables a greater scope throughout the form. However, if you create multiple forms in
a project, the variables declared at the top of a form are valid only in the form in which they
were declared. Likewise, event procedures are by default declared as private and are only
local to the form in which they are created. For example, you can’t call the Button1_Click
event procedure from a second form named Form2 if the event procedure is declared to be
private to Form1. (You’ll learn how to add additional forms to your project in Chapter 14,
“Managing Windows Forms and Controls at Run Time .”)
Table of Contents
Creating Modules and Procedures........................... 247
Working with Modules.............................................. 247
Creating a Module............................................. 248
Working with Public Variables........................................ 251
Creating Procedures................................................ 255
Writing Function Procedures......................................... 256
Function Syntax............................................... 257
Calling a Function Procedure.................................... 258
Using a Function to Perform a Calculation........................ 258
Writing Sub Procedures............................................. 262
Sub Procedure Syntax.......................................... 262
Calling a Sub Procedure........................................ 263
Using a Sub Procedure to Manage Input.......................... 264
One Step Further: Passing Arguments by Value
and by Reference................................................... 268
Chapter 10 Quick Reference.......................................... 270