Writing General Procedures in VB6.0
VISUAL BASIC 6.0
WRITING
GENERAL PROCEDURES
Procedures perform some specific task of the VB Project.
A procedure is a set of VB statement in a larger
Project.
Advantages
- Decomposing complex problems into smaller subproblems
- Reducing duplication of code
- Reuse of code
- Improving readability of the VB Project
Creating
a New Sub Procedure
Steps to add a new general procedure to a form:
- Display the Code window for the form
- Select Add Procedure from the Tools menu
- Enter a name in the Add Procedure dialog box
- Choose the Type of Procedure (Sub/ Function / Property / Event)
- Select Private or Public for Scope of the VB Project
- Clink OK
Types of Procedures
1.Sub Procedures – perform actions but
do not return a value to the calling code
2.Function Procedures – perform
actions and return a value to the calling code
3.Property Procedures – create and execute
custom properties of an objects
4.Event Procedures - execute when a particular event occur on a particular
object
1.Sub
Procedures
- A Sub Procedure is a set of VB statements enclosed by the Sub and End Sub statements.
- The Sub procedure performs a task and then returns control to the calling code.
- But it does not return a value to the calling code.
Syntax for declaring
a Sub Procedure is
[scope]
Sub SubName[(Parameter List)]
Statement of the Sub Procedure
End Sub
The value of scope may be Public or
Private
The Syntax for a call
to a Sub Procedure is
[Call] SubName [(Parameter List)]
Passing Parameters: By Value or By Reference
VB supports two ways
of passing parameters to procedures
By Value (ByVal)
By Reference (ByRef)
By Value (ByVal)
When pass arguments by values, the procedure works only with the copies of the values.
When work with large amount of data, this may lead to performance overheads.
By Reference (ByRef)
When pass arguments by reference, the procedure receives a reference to the actual values.
When modified, the original values are affected.
ByRef consumes more time and space efficient.
But it is more error prone.
Eamples
1. Dim A, B As String
Private Sub
JOIN_Click()
Call add( )
End Sub
Public Sub add( )
A = "Welcome to
"
B = "VB
Programming World"
Label1.Caption = A
& B
End Sub
2. Dim A, B As String
Private Sub
JOIN_Click()
A = "Welcome to
"
B = "VB
Programming World"
call add(A, B)
End Sub
Public Sub add(ByVal
X As String, ByVal Y As String)
Label1.Caption = X
& Y
End Sub
2.Function
Procedure
- A Function Procedure is a set of VB statements enclosed by the Function and End Function statements.
- The Sub procedure performs a task and then returns control to the calling code.
- When it returns control, it returns a value to the calling code.
The Syntax for
declaring a Function is
[scope]
Function FunctionName[(Parameter List)] As ReturnType
Statement of the Function Procedure
End Function
The syntax for a call
to a Function is
[Call] FunctionName [(Parameter List)] (or)
Value = FunctionName [(Parameter List)]
Returning values
The Function
returns a value to the calling code.
Example
Dim A, B As String
Private Sub
JOIN_Click()
A = "Welcome to
"
B = "VB
Programming World"
Label1.Caption =
add(A, B)
End Sub
Public Function add(
X As String, Y As String) As String
Return X & Y
End Function
3.Property
Procedure
- A Property Procedure is a set of VB statements enclosed by the Property and End Property statements.
- A Property procedure is used to create and manipulate custom properties.
- It is used to create read only properties for Forms, Modules and Classes
Type of Property
Procedures
Property Let Procedure – sets the value of a property
Property Get Procedure – returns the value of a property
Property Set Procedure – sets the reference to an object
4.Event Procedures
- An event procedure is a set of statements that execute when a particular event occur on a particular object.
- It contains the control’s actual name, an underscore and the event name.
- The default scope of Event Procedures is Private.
The Syntax for
declaring Event Procedure is
[Scope] Sub ControlName_Event( )
Statement of the Sub Procedure
End Sub
Example
Private Sub
Form_Load()
Caption =
"Event Procedure"
End Sub
Comments
Post a Comment