0% found this document useful (0 votes)
63 views

Visual Basic Procedures

Visual Basic offers different types of procedures and control structures to organize code into logical components. Procedures like sub procedures and function procedures allow breaking code into smaller and reusable pieces. Control structures like If/Then and Select/Case statements allow conditionally executing blocks of code. Arrays provide a way to efficiently store and access multiple values of similar types, and can be fixed size or dynamic to accommodate different needs.

Uploaded by

spy kid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Visual Basic Procedures

Visual Basic offers different types of procedures and control structures to organize code into logical components. Procedures like sub procedures and function procedures allow breaking code into smaller and reusable pieces. Control structures like If/Then and Select/Case statements allow conditionally executing blocks of code. Arrays provide a way to efficiently store and access multiple values of similar types, and can be fixed size or dynamic to accommodate different needs.

Uploaded by

spy kid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

VISUAL BASIC PROCEDURES

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 return 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.

Function Hypotenuse (A As Double, B As Double) As Double


Hypotenuse = sqr (A^2 + B^2)
End Function

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.
CONTROL STRUCTURES

CONTROL STRUCTURES

If…then

If…then…else

Multiple If Statements

Select…case

LOOP STRUCTURES

While loop

Do while

Do until

For…next

Exit

If…then

It is used for conditional execution of one statement

Syntax:

If condition then

Statements

Else

Statements

End if
Example:

If sal > MaxSale Then

Msgbox (“Greater”)

If…then..Else

It is used for conditional execution of one or more statements

Syntax:

If condition then

Statements

Else

Statements

End if

Example:

If sal > MaxSale Then

Msgbox (“Greater”)

Else Msgbox (“lower”)

Endif

Multiple If Statements

It is used for conditional execution of more than one if statements


Syntax:

If condition Then

………

ElseIf condition then

………

Else ………..

End If

Example:

If Bsal > 12000 Then

tSal = 2.5 * Bsal

ElseIf Bsal > 10000 Then

tSal = 2* Bsal

Else tSal = 1.8 * Bsal

End If

Select…case

It is an alternative to if..then…elseif for selectively executing a single block of statements from


among multiple blocks of statements.

Syntax:

Select case index

Case 0

Statements

Case 1

Statements

End select
Example:

AvgNum = total / n

Select Case Round (AvgNum)

Case 100 Grade = “EX”

Case 80 To 99 Grade = “A”

………

End Select

While loop statement (while statement)

It is used to execute statements until a certain condition is met.

Syntax:

While (condition)

Statement

Wend

Example:

Dim I as integer

I=0

While i<10

Msgbox (i) I=i+1

Wend
Do while loop statement (do while statement)

It is used to execute statements first and test the condition after each execution.

Syntax:

Do

Statement

Loop while (condition)

Example:

Dim I as integer

I=0

do Msgbox (i) I=i+1 Loop

While (i<10)

Do Until statement

It executes the statements until the condition is satisfied

Syntax:

Do while/until (condition)

Statement Loop
Example:

Dim I as integer

I=0

Do while/until (i<10)

Msgbox (i)

I=i+1

Loop

For…next loop statement

It is another way to make loops in VB.

For loop statements are written in three steps

o Initialization

o Condition check

o Incrementation

Syntax:

For <variable name><range>

Next
Example

Private sub form_load( )

Dim I as integer

For I = 0 to 10 step 2

Msgbox (i)

Next End sub


ARRAY

As the number of variables is increasing, complexity of the program also increases and
programmers get confused with the variable names.

There may be situations in which we need to work with a large number of similar data
values. To make this work more easily, by using a concept called "Array".

Array
An array is a variable which can store multiple values of the same data type at a time.

"Collection of similar data items stored in contiguous memory locations with single
name".

Syntax:

Array can be declared with the help of keyword “dim”

Dim variable name (value) as data type

Example:

Dim a (10) as integer Dim a (10) as a string

Types of arrays

Two types of arrays

1. Fixed size array

2. Dynamic array
Fixed size array

The size of the array always remains the same

Fixed arrays can be declared by giving a name to the array with the upper limit in the
parenthesis.

Declaration of fixed size array

Dim lengths (9) as integer

In the above declaration

Lengths -> name of the array and

9 -> is the upper limit of the array

In the above declaration creates an array element with index numbers running from 0 to 8
Dim lengths (1 to 9 ) as integer

In the above statement an array of 9 elements is declared but indexes running from 1 to 10

Multidimensional array

It is used when we need to represent or store related information on different dimensions

For example, to hold the student registration number and marks of the student we need to
mention its x and y coordinates
Declaration of multidimensional array

Dim student marks (1 to 100,101 to 200) as integer

The three dimensional array with defined lower limits are given below

Dim student marks (1 to 100,101 to 200) as integer

Dynamic arrays

There will be a situation when the user may not know the exact size of the array at design
time

Under such circumstances, a dynamic array can be initially declared and the user can then
add elements when needed instead of declaring the size of the array at design time

Redim is used to dynamically control or reminsion the size of the array

Declaration of Dynamic array

You can declare the array as dynamic by giving it an empty dimension list Dim newarray
()

Use the Redim statement to allocate the actual number of elements Redim newarray
(y+1)

Redim is an executable statement and can appear only in a procedure It can be used for
fixed arrays is used for declaring ReDim statements

The lower and upper limits for each dimension can also be specified explicitly as in a fixed
size array Redim firstarray (4 to 12)

Each time an executing the ReDim statement, the current data stored in the array is lost
and the default value is set

But if we want to change the size of the array without losing the previous data, we have to
use the

You might also like