Sub ProcedureName()
End Sub
The name of a procedure should follow the same rules
we learned to name the variables. In addition:
- If the procedure performs an action that can be represented with a
verb, you can use that verb to name it. Here are examples: show,
display
- To make the name of a procedure stand, you should start it in
uppercase. Examples are Show, Play, Dispose, Close
- You should use explicit names that identify the purpose of the
procedure. If a procedure would be used as a result of another
procedure or a control's event, reflect it on the name of the sub
procedure. Examples would be: afterupdate, longbefore.
- If the name of a procedure is a combination of words, you should
start each word in uppercase. Examples are: AfterUpdate, SayItLoud
The section between the Sub and the End Sub
lines is referred to as the body of the procedure. Here is an example:
Sub Assign()
End Sub
The body of the
procedure is used to define what, and how, the assignment should be carried.
For example, if you need to use a variable, you can declare it and specify
the kind of variable you need. There is no restriction on the type of
variables that can be declared in a procedure. Here is an example in which
a string variable is declared in the body of a sub routine:
Sub Assign()
Dim strFullName As String
End Sub
In the same way, you can declare as many variables as
you need inside of a procedure. The actions you perform inside of a
procedure depend on what you are trying to accomplish. For example, a
procedure can simply be used to create a string. The above procedure can
be changed as follows:
Sub Assign()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
End Sub
|
Practical
Learning: Introducing Procedures
|
|
- Start Microsoft Visual Basic
- To create a new application, on the main menu, click File -> New ->
Project or File -> New Project
- In the Templates list, click Console Application
- In the Name text box, type Squared
- In the Solution Name text box, type Geometry1
- Click OK
- In the Solution Explorer, under the Square node, right-click Module1.vb and click Rename
- Type Square.vb and press Enter
- To create a procedure, change the document as follows:
Public Module Square
Sub ProcessSquare()
Dim dblSide As Double
Dim dblPerimeter As Double
dblSide = InputBox("Enter Side: ")
dblPerimeter = dblSide * 4
MsgBox("=-= Square Characteristics=-=" & vbCrLf & _
"Side: " & dblSide & vbCrLf & _
"Perimeter: " & dblPerimeter)
End Sub
Sub Main()
End Sub
End Module
|
- Save the document
Once you have a procedure, whether you created it or
it is part of the Visual Basic language, you can use it. Using a procedure is also referred
to as calling it. Before calling a procedure, you should first locate the
section of code in which you want to use it. To call a simple procedure,
type its name followed by parentheses in the section where you want to use
it.
Here is an example:
Module Exercise
Sub Assign()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
MsgBox(strFullName)
End Sub
Friend Sub Main()
Assign()
End Sub
End Module
Besides using the name of a
procedure to call it, you can also precede it with the Call
keyword. Here is an example:
Module Exercise
Sub Assign()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
MsgBox(strFullName)
End Sub
Friend Sub Main()
Call Assign()
End Sub
End Module
|
|