Creating Modules and Procedures
Modules are separate sections of program code.
The module is a separate file with a .VB extension. It contains
variable declarations and program statements just like in the main
program, but it can be executed with a single statement from anywhere
in the program.
The scope of a variable becomes an issue when you
begin to work with modules. Variables may be “seen” by the program
only within a module, or throughout all the programs module. The
variables only seen within the module are called local variables.
This variation is the default. The variables which can be seen,
assigned values and used by any module within the program are called
public variables.
Creating a module
- Pull down the Project Menu, choose Add
Module.

- The Module icon should be automatically
selected.
- Type a name in the textbox at the bottom of
the window.
- Click Add.

The module will open in the IDE in the code view.
Declaring public variables
Variables which retain a value throughout the
program, and that can be used anywhere in the program are called
public variables. Their scope extends through the main code and all
other modules. The declaration statement uses the Public keyword
rather than Dim, which is used for local variables. This statement
can be at the top of the form’s program code, or within the module
where it first occurs in code. I think the top of the form’s code is
best. Using this scheme, all public variables are in the same
location.
Syntax:
Public variableName As
Type
Example:
Public Class Form1
Public RunningTotal As
Interger
Procedures
A procedure is the set of statements that perform
a unique task. The tasks are divided into two general categories by
the type of task– those that do calculations and those that receive or
process input, display output or set properties.
Procedures which perform a calculations are
called Functions. A function receives a variable or number(the
argument) and return a modified value.
The other category of procedures are called Sub
Procedures. They also revieve arguments and return modified values in
an argument list
Procedures are useful because the same task may
be required at several points in a program. Using a procedure allows
you to create the code once which saves development time.
Creating a function
Syntax:
Function FunctionName([arguments]) As Type
Function statements go here
[return value]
End Function
FunctionName is the name you have the function.
As Type declares the type of output the function
will generate.
Arguments is a list of arguments to be used in
the function. They are optional and are separated by commas. The type
must be included here as well.
The last line is usually an assignment statement.
Return Value is a list of arguments to be passed
back from the function. This is optional. Separate these with
commas. Any code following this line is ignored.
The last line is usually an assignment statement.
End Function separates this function’s code from
what follows.
Example:
Function TotalTax(ByVal Cost as Single) as
Single
Dim StateTax, CityTax as Single
StateTax = Cost * 0.05
CityTax = Cost * 0.15
TotalTax = StateTax + CityTax
End Function
Notice the text ByVal in the arguments section.
This was not mentioned the Syntax description. It tells VB to pass
the value of the variable Cost to the function.
The function call
Somewhere in the program this statement occurs
and causes the procedure to execute using the number 500 as the value
for the variable Cost.
lblTaxes.Text =
TotalTax(500)
This causes the function TotalTax to run using
the value 500 as the contents for the variable Cost. The result will
be returned, which is the total tax on a purchase of $500.
Function calls usually use a variable, like this:
lblTaxes.Text =
TotalTax(SalesTotal)
Creating a Sub Procedure
Sub procedures usually return input from a user,
display or print something or change properties when a condition
occurs.
Syntax
Sub ProcedureName([arguments])
Procedure statements go here
End Sub
ProcedureName is what you name the procedure.
Arguments is a list of items, optional and
separated by commas, which will be used by the sub procedure.
The procedure statements are program statements,
just like any others within your program, which are required to get
the task accomplished.
Example:
Sub Greeting(ByVal FirstName as String)
Msg = “Happy Birthday “ &
FirstName
MsgBox(Msg,,”Have a good day!”)
End Sub
The example creates a message Happy Birthday
concatenated with the FirstName variable contents. That message is
concatenated with the message “Have a great day!”. The entire message
is displayed in a message box.
This sub procedure is called by a statement like
this:
FriendlyGreeting(FirstName)
ByRef and ByVal
When arguments may be passed ByRef. This means
the location in memory is passed back (ref). With this technique, any
changes that happened to the variable, as a result of the sub
procedure will be passed. The sub procedure is allowed to change the
value of the variable.
When arguments are passed by value, the sub
procedure cannot make any changes to the variable’s value. |