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

Unit-5 CC

Uploaded by

Hiteshi Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit-5 CC

Uploaded by

Hiteshi Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT 5 FUNCTIONS & PROCEDURES

Structure
5.0 Introduction
5.1 Objectives
5.2 Functions and Procedures
5.3 Form, Standard & Class Module
5.4 Sub Procedure
5.5 Do-Event Functions
5.6 Control Arrays
5.7 Summary

5.0 INTRODUCTION

The Visual Basic language program comprises of certain building blocks of Visual Basic
programming constructs. These constructs include statements, procedures, functions, events etc.
Now you are familiar with various kinds of controls and available in Visual Basic. In this chapter
we will be looking at the user-defined functions and procedures.

After going through this unit, you will be able to:

Define a function, sub procedure, do event functions,

Use control arrays.

5.2 FUNCTIONS AND PROCEDURES

A Function is a set of statements (code that someone writes) that perform a specific task within a
program and return either a number or a string value. Visual Basic has number of functions. A
Function begins with a Function statement and ends with an End Function statement. A function
may also accept arguments. These arguments are enclosed in parentheses () following the function
name.

A Procedure is a sequence of code statements that performs a specific task within a program but
return no explicit value. These statements are executed as a unit. A sub procedure begins with a Sub
statement and ends with an End Sub statement. Sub procedure performs actions but do not return
any values whereas Function procedures perform actions and return values.

A Function.or Procedure tells an application what and how to perform a specific task when an event
is generated. Once the Function or Procedure is defined, it must be explicitly invoked by the
application. An event procedure remains idle until called upon to respond to events caused by the
user. You can place a Function or Procedure in either a form module, standard module, or class
mudule.
r uncrlons ai
5.3 FORM, STANDARD & CLASS MODULE Procedures

Form module: A Form module is a file in Visual Basic project with a .FRM filename extension
that can contain graphical descriptions of a form, its controls and property settings, form level
declarations of constants, variables and external procedures, and event and procedures.

Standard module: Standard module is a module containing only procedure, type and data
declarations and definitions. Module level declarations and definitions in a standard module are
Public by default.

Class module: A module that contains the definition of a class. Functions or Procedures can be
either public or private in this module. Public Functions or Procedures can be accessed from any
module in an application, provided you qualify the name of the Function or Procedure with the
name of the module that contains the Function or Procedure. Functions or Procedures can help
divide complex application code into more manageable units.

SUB PROCEDURE

A Procedure is a sequence of code that performs a specific task within a program but returns no
explicit value. A Sub procedure begins with a sub statement and ends with an End Sub statement.
Code within a module is organised into procedures. A procedure tells an application how to perform
a specific task.

To create a procedure
1 . Open the module (either Form, Standard or Class) for which you want to write the procedure.
2. Type Sub and the name of the procedure.
3. Type in sequence of code statements for the procedure.
Visual Basic concludes the procedure with the appropriate End Sub statement.

L
Syntax

[Private 1 Public] [Static] Sub name [(argument list)]

[statements]

[statements]

End Sub

To create a procedure using the Insert Procedure dialog box


1 . Open the module for which you want to write the procedure.
2. Enter the name of the procedure in the Name box provided.
3. Select the type of procedure you want to create i.e. Sub, Function, or Property.
4. Set the procedure's scope to either Public or Private.
<:urnpunents of
Windows
Programming procedure definition.
& Visual Basic
6. Choose OK to insert the Procedure tem~latein the Code window.

This function on execution performs action so that the operating system can process other events.
This function returns the number of open forms.

DoEventsO passes control to the operating system. Control is not returned until the operating
system has finished processing the events in its queue and all keys in the SendKeys queue have
been sent.

If parts of your code take up a lot of processor time, use DoEvents periodically to relinquish control
to the operating system so that events, such as keyboard input and mouse clicks, can be processed
without significant delay.

Make sure the procedure that has given up control with DoEvents is not executed again from a
different part of your code before the first DoEvents call returns; this could cause unpredictable
results. In addition, do not use DoEvents if other applications could possibly interact with your
procedure in unforeseen ways during the time you have yielded control. 1

CONTROL ARRAYS

An array is a set of variables of the same data types. These are used to group related variables
together i.e. all command buttons, all check boxes etc.

A control array is a group of controls that share the same Name, type, and event procedures. A I

control array has at least one element and can grow to as many elements as your system resources i
and memory permit. The size depends on how much of memory and Windows resources each
control requires. The maximum index you can use in a control array is 32767 with 32-bit Windows
platforms, and approximately 16006 with 16-bit Windows platforms.

There are two ways to create a control array: Assign the same name to more than one object of the
same type and Copy an existing object and paste it on the form.

To create a control array by the first way, follow the steps:

1. All objects of the same type are drawn that are to be grouped together in a control array.

2. Select the object that should be the first element in an array. 1

3. Select View -> Properties to open the Properties Window.

4. Give the name to the control array in the box specified.

5. Put the next object and so on till all objects have been selected.

6. Click YES to create a control array.


To create a control array in the second way, follow the steps: Functions &
Procedures

1 . Draw the first object that is to be grouped in a control array.

2. Select View -> Properties to open the Properties Window.

3. Give the name at Name property.

4. Select other object to be formed part of the group.

'I 5. Select Edit -> Copy from the menu bar.

6. Select Edit -> Paste from the menu bar. Answer YES if you want to make this new object part
of a control array.

I 7. Follow the above procedure till all the objects are selected. There is no limit to the number of
objects as such.

Once the control array is created, no change is to be made to the name of an object otherwise the
new object that is created by changing the name of the object ceases to be part of the control array.

Elements of the same control array have their own property settings. Common uses for control
arrays include Menu controls and groups of OptionButton controls.

Control arrays are useful if one wants several controls to share the code. For example, if you create
three OptionButton controls as a control array, the same code executes regardless of which button
receives the Click event. If one wants to create a new control at run time, that control must be a
member of an existing control array. With a control array, each new element inherits the common
event procedures of the array.

Without the control array mechanism, creating new controls at run time is not possible, because a
completely new control would not have any predefined event procedures. Control arrays solve this
problem, because each new control inherits the common event procedures already written for the
array. For example, if your form has several TextBox controls that each receives a date value, you
can set up a control array so that all of the TextBox controls share the same validation code.
Components of
Windows Check Your Progress
Programming
&Visual Basic 1. Create the following form

The application will contain three image controls, a timer object and a command button. Tlie
properties and settings of each object will be

Object Property Setting

Form Name PhoneForm

Caption "Phone"

Command Button Name Command 1

Cancel - 1 True
Caption "Exit"

Timer Name Timer 1

Interval 500

Image Name Main

Picture c:\phone 1

Image Name Ring 1

Picture c:\phone2

Image Name Ring2

Picture c:\phone3

Procedure for Timer Event


Private Sub Timerl-Timer() Functions &
Procedures

Static Pbmp As Integer

Main.Move Main.Left+l S,Main.Top-6

if Pbmp Then

Main.Picture=RingZ.Picture

Else

Main.Picture=Ring 1 .Picture

Endlf

Pbmp=Not Pbmp

End Sub

SUMMARY

In this chapter, you have been given exposure to Functions, Procedures and control arrays:

1 . Function or Procedure is a named sequence of statements.

2. A Procedure performs a specific task within a program but returns no explicit value.

3. A procedure perform a specific task within a Visual Basic program and returns a value.

4. A Function or Procedure tells an application what and how to perform a specific task when an
event is generated.

5. An event procedure remains idle until called upon to respond to events caused by the user.

6. A control array is a group of controls that share the same Name, type, and event procedures.

7. There are two ways to create a control array: Assign the same name to more than one object of
the same type and Copy an existing object and paste it on the form.

You might also like