Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.

46
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures

Transcript of Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.

Microsoft Visual Basic 2010: Reloaded Fourth Edition

Chapter EightSub and Function Procedures

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Objectives

After studying this chapter, you should be able to:

• Explain the difference between Sub and Function procedures

• Create a Sub procedure

• Pass information to a procedure

• Explain the difference between passing data by value and passing data by reference

• Explain the purpose of the sender and e parameters

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Objectives (cont'd.)

• Associate a procedure with more than one object and event

• Create a Function procedure• Convert an Object variable to a different type using

the TryCast operator• Utilize a timer control

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Procedures

• Procedure: a block of program code that performs a specific task

• Two types of procedures:– Function procedure: returns a value after performing

its task– Sub procedure: does not return a value

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Sub Procedures

• Two types of Sub procedures: event procedures and independent Sub procedures

• Event procedure: – Sub procedure that is associated with a specific

object and event– Automatically processed when the associated event

occurs• Independent Sub procedure:

– Collection of code that can be invoked from one or more places in an application

– Not associated with an event– Processed only when called (invoked)

Sub Procedures (cont'd.)

• Independent Sub procedures are used to:– Eliminate the duplication of code in different parts of

a program– Allow a large, complex application to be broken into

small and manageable tasks– Allow multiple programmers to work on an

application simultaneously

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-1: How to create an independent Sub procedure

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Sub Procedures (cont'd.)

• Independent Sub procedures:– Have a procedure header and procedure footer– Use Pascal case for names– Optionally contain a parameter list

• Parameters: – Declared in the procedure header– Store the information passed into the procedure when

it is invoked

• Call statement: invokes an independent Sub procedure

Sub Procedures (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-2: How to call an independent Sub procedure

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Gadis Antiques Application

Figure 8-3: Sample run of the Gadis Antiques application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-4: Partial code for the Gadis Antiques application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Including Parameters in an Independent Sub Procedure

• Parameter: stores data that is passed to the procedure when the procedure is invoked

• When calling a procedure with parameters, you must pass:– The same number of arguments– The same type of arguments– The arguments in the same order as declared in the

procedure

• Can pass a variable, named constant, literal constant, or keyword as parameter

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Passing Variables

• Each variable has a value and a unique memory address

• Variable can be passed to a procedure in two ways:– By value: you pass the variable’s value– By reference: you pass the variable’s address

• Passing by value: the procedure receives only the value and cannot change the actual variable’s value

• Passing by reference: the procedure receives the address and can make changes to the variable’s value

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Passing Variables by Value

• Use the keyword ByVal before the parameter in the procedure declaration

• ByVal is the default method of passing variables

• Procedure cannot change the actual variable’s value

Figure 8-5: Sample run of the Pet Information application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-6: Partial code for the Pet Information application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Passing Variables by Reference

• Use the keyword ByRef before the parameter in the procedure declaration

• Procedure receives the address of the variable and is able to change the variable’s value

Figure 8-7: Sample run of the Gross Pay application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-8: CalcGrossPay procedure and calcButton_click event procedure

Passing Variables by Reference (cont’d.)

• Desk-checking: the process of reviewing the program instructions while seated at your desk instead of in front of a computer– Also called hand-tracing

• Choose a set of sample data for input values

• Manually compute the expected output values

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-9: Gross pay calculation using sample input values

Figure 8-10: Desk-check table before the Call statement is processed

Passing Variables by Reference (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-11: Desk-check table after the Call statement and CalcGrossPay procedure header are processed

Passing Variables by Reference (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-12: Desk-check table after the first statement in the CalcGrossPay procedure is processed

Passing Variables by Reference (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-13: Desk-check table after the statement in the selection structure’s true path is processed

Passing Variables by Reference (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-14: Desk-check table after the CalcGrossPay procedure ends

Passing Variables by Reference (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Associating a Procedure with Different Objects and Events

• Handles keyword:– Appears in event procedure header– Indicates the object and event associated with the

procedure– Controls when the procedure is invoked

• By default, the event procedure name matches the name of the associated object and event

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-15: Some of the Gadis Antiques application’s code from Figure 8-4

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Associating a Procedure with Different Objects and Events (cont'd.)

• Event procedure:– Name of event procedure can be changed– Can be associated with more than one object and

event as long as each event has the same parameters

• Add the additional object/events to the Handles clause

• Sender parameter: contains the memory address of the object that raised the event

• e parameter: contains additional information about the object that raised the event

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-16: ClearLabels procedure

Associating a Procedure with Different Objects and Events (cont'd.)

Function Procedures

• Function procedure (or Function): – Block of code that performs a specific task– Returns a value after completing its task

• Visual Basic contains many built-in functions

• You can create your own functions with or without parameters

• A function is invoked by including its name with any arguments in a statement

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Function Procedures (cont'd.)

• Function procedure header: – As datatype clause indicates the type of the return

value• Function procedure footer statement:

– End Function• Return keyword:

– Sets the value to be returned by the function– Ends the function

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-17: How to create a Function procedure

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-18: How to invoke a Function procedure

Function Procedures (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

The Circle Area Calculator Application

Figure 8-19: Sample run of the Circle Area Calculator application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-20: Partial code for the Circle Area Calculator application

The Circle Area Calculator Application (cont’d.)

Converting Object Variables

• sender parameter in an event procedure is declared as type Object– Contains the address of the object that raised the

event

• To access the properties of the object, you must convert it to the appropriate data type

• Type casting (or casting): the process of converting a variable from one data type to another

• TryCast operator: used to cast a variable to a different data type

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-21: How to use the TryCast operator

The Full Name Application

• User enters a first and last name

• Application concatenates both names and displays results

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-22: Sample run of the Full Name application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 8-23: Two ways of writing the Enter event procedures for both text boxes

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Using a Timer Control

• Timer control: processes code at one or more regular intervals

• Timer control does not appear on the form, but in the component tray

• Component tray: stores controls that do not appear in the user interface at run time

• Interval property: specifies the length of time (in milliseconds) until the next Tick event occurs

• Tick event: occurs after each interval has elapsed• Enabled property: must be True for timer to run

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 24: Interface for the Timer Example application

Using a Timer Control (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Using a Timer Control (cont'd.)

Figure 8-25: Code entered in two of the application’s procedures

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Tutorial 1

• The Tri-County Electricity Application

Figure 8-27: MainForm in the Tri-County Electricity application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Tutorial 2

• The Concentration Game

Figure 8-35: MainForm for the Concentration Game application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Programming Example

Figure 8-47: MainForm in the Rainfall Calculator application

• Rainfall Calculator Application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary

• Function procedures return a value; Sub procedures do not return a value

• Event procedure: a Sub procedure associated with one or more objects and events

• Independent Sub and Function procedures: not associated with any specific object or event

• Call statement: used to invoke a procedure

• When calling a procedure, you must pass the same number, type, and order of parameter values as those declared in the procedure

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary (cont'd.)

• Values can be passed to a procedure by value or by reference

• By Value:– Provides only the value of the variable to the

procedure– Use the ByVal keyword

• By Reference: – Provides the address of the variable to the

procedure, allowing the procedure to change the variable’s value

– Use the ByRef keyword

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Summary (cont'd.)

• Variables in the parameter list in a procedure header have procedure scope

• Can modify the Handles clause of an event procedure to associate it with more than one object and event

• TryCast keyword: allows you to convert an object from one data type to another

• Timer control: used to process code at one or more specified intervals