Cit2457 VB Unit 3
Cit2457 VB Unit 3
PROCEDURE
Visual Basic offers different types of procedures to execute small sections of coding in
applications. The various procedures are elucidated in details in this section. Visual Basic
programs can be broken into smaller logical components called Procedures. Procedures are
useful for condensing repeated operations such as the frequently used calculations, text and
control manipulation etc. The benefits of using procedures in programming are:
It is easier to debug a program a program with procedures, which breaks a program into
discrete logical limits.
Procedures used in one program can act as building blocks for other programs with slight
modifications.
A Procedure can be Sub, Function or Property Procedure.
Sub Procedures
A sub procedure can be placed in standard, class and form modules. Each time the
procedure is called, the statements between Sub and End Sub are executed. The syntax for a sub
procedure is as follows:
[Private | Public] [Static] Sub Procedurename [( arglist)]
[ statements]
End Sub
arglist is a list of argument names separated by commas. Each argument acts like a variable in
the procedure. There are two types of Sub Procedures namely general procedures and event
procedures.
Event Procedures
An event procedure is a procedure block that contains the control's actual name, an
underscore(_), and the event name. The following syntax represents the event procedure for a
Form_Load event.
Private Sub Form_Load()
....statement block..
End Sub
Event Procedures acquire the declarations as Private by default.
General Procedures
A general procedure is declared when several event procedures perform the same actions. It is a
good programming practice to write common statements in a separate procedure (general
procedure) and then call them in the event procedure.
In order to add General procedure:
The Code window is opened for the module to which
the procedure is to be added.
The Add Procedure option is chosen from the Tools
menu, which opens an Add Procedure dialog box as
shown in the figure given below.
The name of the procedure is typed in the Name
textbox
Under Type, Sub is selected to create a Sub procedure, Function to create a Function
procedure or Property to create a Property procedure.
Under Scope, Public is selected to create a procedure that can be invoked outside the
module, or Private to create a procedure that can be invoked only from within the
module.
We can also create a new procedure in the current module by typing Sub ProcedureName,
Function ProcedureName, or Property ProcedureName in the Code window. A Function
procedure returns a value and a Sub Procedure does not return a value.
Function Procedures
Functions are like sub procedures, except they return a value to the calling procedure. They are
especially useful for taking one or more pieces of data, called arguments and performing some
tasks with them. Then the functions returns a value that indicates the results of the tasks
complete within the function. The following function procedure calculates the third side or
hypotenuse of a right triangle, where A and B are the other two sides. It takes two arguments A
and B (of data type Double) and finally returns the results.
The above function procedure is written in the general declarations section of the Code window.
A
function can also be written by selecting the Add Procedure dialog box from the Tools menu and
by choosing the required scope and type.
Property Procedures
A property procedure is used to create and manipulate custom properties. It is used to create read
only properties for Forms, Standard modules and Class modules. Visual Basic provides three
kind of property procedures-Property Let procedure that sets the value of a property, Property
Get procedure that returns the value of a property, and Property Set procedure that sets the
references to an object.
Built – in - Functions
The mathematical functions are very useful and important in programming because very often
we need to deal with mathematical concepts in programming such as chance and probability,
variables, mathematical logics, calculations, coordinates, time intervals and etc.
The common mathematical functions in Visual Basic are Rnd, Sqr, Int, Abs, Exp, Log, Sin,
Cos, Tan , Atn, Fix and Round.
(i) Rnd is very useful when we deal with the concept of chance
and probability. The Rnd function returns a random value
between 0 and 1.
Example 1:
Dim num as integer
Private Sub Command1_Click ( )
Num=Int(Rnd*6)+1
Label1.Caption=Num
End Sub
Now, run the program and then click on the roll die button, you will get an output like the figure
below:
Numeric Functions
The numeric functions are Int, Sqr, Abs, Exp, Fix, Round and Log.
a) Int is the function that converts a number into an integer by truncating its decimal part and the
resulting integer is the largest integer that is smaller than the number. For example, Int(2.4)=2,
Int(4.8)=4, Int(-4.6)= -5, Int(0.032)=0 and so on.
b) Sqr is the function that computes the square root of a number. For example, Sqr(4)=2,
Sqr(9)=2 and etc.
c) Abs is the function that returns the absolute value of a number. So Abs(-8) = 8 and Abs(8)= 8.
e) Fix and Int are the same if the number is a positive number as both truncate the decimal part
of the number and return an integer. However, when the number is negative, it will return the
smallest integer that is larger than the number. For example, Fix(-6.34)= -6 while Int(-6.34)=-7.
f) Round is the function that rounds up a number to a certain number of decimal places. The
Format is Round (n, m) which means to round a number n to m decimal places. For example,
Round (7.2567, 2) =7.26
g) Log is the function that returns the natural Logarithm of a number. For example,
Log 10= 2.302585
String Functions
The string function are Len, Right, Left, Mid, Trim, Ltrim, Rtrim, Ucase, Lcase, Instr, Val,
Str ,Chr and Asc.
Control Structures
Control Statements are used to control the flow of program's execution. Visual Basic supports
control structures such as if... Then, if...Then ...Else, Select...Case, and Loop structures such as
Do While...Loop, While...Wend, For...Next etc.
LOOPS
Visual Basic allows a procedure to be repeated as many times as the processor can support. This
is generally called looping. Looping is known as a repetitive structure. (an action is to be
repeated until given condition is true).
There are several formats for a do loop
General Syntax
Do While <contidion>
Statements
Increment
Loop
eg:
Dim number As Integer
number = 1
Do While number <= 100
number = number + 1
Loop
A variable number is initialized to 1 and then the Do While Loop starts. First, the condition is
tested; if condition is True, then the statements are executed. When it gets to the Loop it goes
back to the Do and tests condition again. If condition is False on the first pass, the statements
are never executed
Do Until...Loop Statement
Unlike the Do While...Loop structures, the Do Until... Loop structure tests a condition for falsity.
Statements in the body of a Do Until...Loop are executed repeatedly as long as the loop-
continuation test evaluates to False.
Syntax
Do Until <condition>
Statements
Increment
Loop
eg:
Dim number As Long
number=0
Do Until number > 1000
number = number + 1
Print number
Loop
Syntax
For <variable> = st.value to end_value step <step_value>
Statement
Next
Dim x As Integer
For x = 1 To 10
Print x
If x = 5 Then
Print "The program exited at x=5"
End If
Next
The Following statement blocks containing Do...While loop is terminated using Exit Do
statement.
Dim x As Integer
Do While x < 10
Print x
x=x+1
If x = 5 Then
Print "The program is exited at x=5"
Exit Do
End If
Loop