There are several different kinds of procedures we can use in Visual Basic. Some of them are:
Event procedures tell objects how to respond to events. So far most of the code we have written has been placed in Click Event/Handlers. Event procedures can be initiated (made to execute) by actions of the user or by something that happens in the code. (The Focus method will cause the related GotFocus or Enter event to fire.)
General procedures are programmer defined. That is they are created by the programmer and initiated by the programmer. There are two kinds of general procedures
Sub procedures
Function procedures
The difference between Sub procedures and Function procedures is that Function procedures return a value and Sub procedures do not.
We have made use of at least one language supplied function in the code we have written so far - IsNumeric( ). Some of you have used UCase( ), LCase( ), CInt( ), CDec() and CStr( ). All of these functions returned something that we used in our code.
We have also used Methods (.ToString( ), .ToUpper, ToDecimal( ), etc. that return values. Methods that return values are functions that are associated with an object.
All computer languages use procedures but they may call them something different. All of the following can be considered synonyms for procedure in some computer language.
I will use the general term subroutine for the sake of the following discussion
A subroutine is a group of related instructions that can be initiated by “Calling” the subroutine from anywhere in the program. There are many benefits for using subroutines.
Subroutines make it easier to see “the big picture” by placing detail level instructions away from the skeleton (the main logic) of the program.
Different programmers can be assigned the task of creating subroutines.
Subroutines may be called from multiple places in a program or reused in other programs.
When creating subroutines each subroutine should be charged with one basic task. You should be able to describe the purpose of the subroutine in the name of the subroutine.
You, the programmer, supply the names for subroutines. Just like you declare variables with self-documenting names you should give your procedures names that are descriptive. A good rule to follow is the verb-noun or verb-adjective-noun guideline.
CalculateGrossPay
DisplayAverage
DetermineMonthlyRent
PrintAmortizationSchedule
ClearLabels
If you cannot describe the purpose of the procedure in three words you should consider breaking the subroutine into smaller subroutines.
In Visual Basic a subroutines / general procedure can be declared
Private Sub InitializeVariables()
:
:
End Sub
and then called from anywhere in the class
Call InitializeVariables( )
The word ‘Call’ is optional and is generally omitted.
InitializeVariables( )
The parenthesis following the procedure name are required but will be automatically supplied if you leave them off.
When you call a general procedure control is passed to the top of the general procedure, statements are executed in the order they occur and when the end of the procedure is reached control passes back to the statement following the calling statement.
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
InitializeVariables( )
txtName.Focus( ) ‘this line will be executed after the
InitializeVariables procedure is completed
:
:
When to use subroutines or general procedures
Subroutines are used to break complex sections of code into smaller more manageable pieces and to prevent very long procedures.
Any time you find the need for 3 or more statements to be repeated in your program you should consider using a subroutine.