Procedures in Visual Basic
-------------------------------------------------------------------------------
------------------------------------------------
In Visual Basic, there are three groups of procedures: Sub procedures, Function procedures, and Property procedures. Their distinguishing characteristics are as follows:
Function
procedures return a value.Sub procedures don't return a value.
Property procedures can return values, assign values, and set references to objects.
-----------------------------------------------
Sub procedures (subroutines) come in two flavors: event and general. Event procedures are Sub procedures executed in response to a user or system event. General procedures are Sub procedures that say how to perform a particular task.
The syntax for a Sub procedure is:
[Private|Public][Static]Sub procedurename (arguments)
statementblock
End Sub
When the procedure is called into action, the statements in the procedure are executed. The arguments in the procedure are the "variables" and are considered to be local, in that they are recognized only in the procedure where they are declared. Unless otherwise specified, they are re-initialized when the procedure is called. You declare the variables using either the Dim or Static keywords.
Dim is used if you need to preserve their values only as long as the procedure is executing. Static is applied when you wish to preserve their values for as long as the application itself is running. In either case, local variables are useful for temporary computation. In fact, you can apply the same name in many different procedures, because each procedure recognizes only its own version of the variable.
You preserve the value of any specific local variable by declaring it static inside the procedure. But if you want to preserve all of the local variable in a procedure, you can put the static keyword at the beginning of a procedure heading (outside). The difference would be as in the following lists.
' Single local variable (MyAccount) is preserved:
Sub Form_Click ()
Static MyAccount
MyAccount = 30
End Sub
' All local variables (MyAccount and YourAccount) are preserved:
Static Sub Form_Click
MyAccount = 30
YourAccount = 45
End Sub
Event procedures are executed in response to mouse-related events. They may be initiated either by the user or by commands in other procedures (system events). That is, a procedure you initiate with an event may itself cause other events to occur.
For example, an event procedures to let you manipulate study conditions for a baseball throw from the outfield to home plate, could be located in a form module for that form. The event procedures for any additional forms for the application would be in their separate form modules. Each event procedure is a program unit, having a beginning and an end.
All event procedures use the same general format, but event procedures for a control use the control name as identifier, whereas an event procedure for a form uses only the general term Form as the (self or default) identifier:
Control Event:
Private Sub
controlname_eventname (arguments)statementblock
End Sub
Form Event:
Private Sub Form
_eventname (arguments)statementblock
End Sub
Example:
The code in the following list is a simple Sub procedure carried out as a result of a click event on Command1, which is a command button on a form, which I have called WorkPage.
Sub Command1_Click()
'This is a click event on Command1 button.' Turns off Circle1, making it invisible
WorkPage.Pic.Circle1.Visible = False
End Sub
This procedure renders false the visibility property of Circle1, a circle control located in a picture box on WorkPage, so it turns the circle off (makes it invisible). The single line of code in the procedure identifies the circle as the one with the name Circle1. It is the circle in the picture box with the name Pic, which in turn is on the form called WorkPage. As normal, the apostrophe (') replaces REM to identify program remarks. (The Private keyword was left off the procedure heading. The reason is that event procedures are Private by default.)
2. General Procedures
General procedures are procedures not directly connected to events. They tell an application how to perform a specific task and can be put in any of the three modules. By default, they are Public in all modules, which means they can be called from anywhere in the application.
Visual Basic provides two ways to call a Sub procedure. One uses the Call syntax, and the other leaves it out. For example, the following two statements call a Sub procedure named Support:
Call Support (Arguments)
Support Arguments
When the Call keyword is used explicitly, the arguments are enclosed in parentheses; otherwise, they are not.
---------------------------------------------
Function procedures have the traditional programming role of putting mathematical functions into play. They can take arguments, perform a series of statements, and change the value of the arguments. Public by default, a Function procedure has the following syntax:
[Private|Public][Static]Function
procedurename (arguments) [As type]statementblock
End Function
To call a function procedure, you simply use its name in an expression, as you would when calling ordinary intrinsic functions, like Cosine or Sine. To call the Cosine function and a function procedure named Pressure, for example, you might simply write:
x = Cos(A)
and
y = Pressure.
----------------------------------------------
A Property procedure is used to create and manipulate custom properties. There are three kinds of Property procedures, and, in either case, the created property becomes a property of the module containing the procedure. The types are Let, Get, and Set:
Property Let
sets the value of a property.Property Get returns the value of a property.
Property Set establishes a reference to an object.
The syntax for a Property procedure is as follows:
[Public|Private][Static] Property [Get|Let|Set] propertyname [(arguments)][As type]
statementblock
End Property
Calling a Property procedure is accomplished in the way shown in the following table.
|
Procedure |
Syntax |
|
Property Let |
[object.]propertyname[(argument)] = argument |
|
Property Get |
variable = [object.]propertyname[(arguments)] |
|
Property Set |
Set [object.]propertyname[(arguments)] = variable |
Format for Property procedures.
-----------------------------------------------
The following demonstrate the use of special and general procedures to compute the value of a pressure X and compare it with a constant called Max. If X exceeds Max, a warning alarm sounds. If the testis iinitiated by the user, it could have the simple form as shown:

A sample program that tests pressure.
A Sub procedure in response to a click event to find the pressure could be written as in the following list.
Sub TestPressure_Click()
X = Hypotenuse (A, B) ' Call for hypotenuse
Text1.Text = X ' Show value of X in the text box
If X > Max Then ' Test X for excessive value
WarningBeeps (NBeeps) ' Call for warning beeps
End If
End Sub
This procedure calls two general procedures: the hypotenuse function and the warning beeps procedure. To compute the hypotenuse and to generate the warning, we could construct the general procedures shown in the next two lists.
Function Hypotenuse (A, B)
Hypotenuse = Sqr(A^2 + B^2) ' Compute the hypotenuse
End Function
Sub WarningBeeps (Nbeeps)
NBeeps = 10 ' Set constant
For i = 1 to NBeeps
Beep ' Sound 10 beeps
Next I
End Sub
In these procedures, the Command button, named TestPressure, like its caption, is clicked to generate the event. The code first calls the Function procedure Hypotenuse, which is executed to return the value for X. The value is displayed in the text box and then compared with the constant Max. If X exceeds Max, the WarningBeeps procedure is called to generate a rapid succession of N warning beeps. In this example, X has the value 4.2, because I set both A and B equal to 3, quite arbitrarily. (The variables are presumed to be computed in another part of a larger program.) The warning occurs because I also set Max equal to 3, again arbitrarily.
--------------------------------