Top Banner
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures
53

Chapter 7: Sub and Function Procedures

Jan 22, 2016

Download

Documents

mahdis

Chapter 7: Sub and Function Procedures. Previewing the Cerruti Company Application. Figure 7-1 Interface showing the payroll calculations. Previewing the Cerruti Company Application (cont.). Figure 7-2 Message box containing a confirmation message. Lesson A Objectives. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012

Chapter 7: Sub and Function Procedures

Page 2: Chapter 7: Sub and Function Procedures

Previewing the Cerruti Company Application

Programming with Microsoft Visual Basic 2012 2

Figure 7-1 Interface showing the payroll calculations

Page 3: Chapter 7: Sub and Function Procedures

Previewing the Cerruti Company Application (cont.)

Programming with Microsoft Visual Basic 2012 3

Figure 7-2 Message box containing a confirmation message

Page 4: Chapter 7: Sub and Function Procedures

Lesson A Objectives

After studying Lesson A, you should be able to:• Create and call an independent Sub procedure• Explain the difference between a Sub procedure and a

Function procedure• Create a procedure that receives information passed to

it• Explain the difference between passing data by value

and passing data by reference• Create a Function procedure

Programming with Microsoft Visual Basic 2012 4

Page 5: Chapter 7: Sub and Function Procedures

• Procedure– A block of program code that performs a specific task

• Two types of Sub procedures in Visual Basic:– Event procedure

• A procedure associated with a specific object and event– Independent Sub procedure

• Independent of any object and event• Processed only when called (invoked) by a Call statement

Programming with Microsoft Visual Basic 2012 5

Sub Procedures

Page 6: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 6

Sub Procedures (cont.)

Figure 7-3 Syntax and examples of an independent Sub procedure and the Call statement

Page 7: Chapter 7: Sub and Function Procedures

• Passing by value– Passes a copy of the

variable’s value– Example: Disclosing your

bank account balance

• Passing by reference– Passes a variable’s

address– Example: Disclosing your

bank account number

Programming with Microsoft Visual Basic 2012 7

Passing Variables

Figure 7-4 Illustrations of passing by value and passing by reference

Page 8: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 8

Passing Variables (cont.)

Passing Variables by Value• Provides only the contents of the variable to the

receiving procedure• How to pass by value:

– Include the keyword ByVal before the parameter • Reasons to pass by value:

– The procedure needs to know the contents of the variable

– The procedure does not need to change the original value• By default, Visual Basic passes by value

Page 9: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 9

Passing Variables (cont.)

Figure 7-5 Favorite Title application’s interface

Figure 7-6 Partially-coded btnDisplay_Click event procedure

Passing Variables by Value (cont.)

Page 10: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 10

Passing Variables (cont.)

Figure 7-8 Message shown in the interface

Figure 7-7 DisplayMsg procedure

Passing Variables by Value (cont.)

Page 11: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 11

Passing Variables (cont.)

Figure 7-9 DisplayMsg procedure header and Call statement

Passing Variables by Value (cont.)

Page 12: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 12

Passing Variables (cont.)

Passing Variables by Reference• Provides the address (memory location) of the

variable to the procedure– The receiving procedure can thus access the variable

• Reason to pass by reference:– The procedure needs to change the variable’s contents

• How to pass by reference:– Include the keyword ByRef before the parameter

Page 13: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 13

Passing Variables (cont.)

Figure 7-11 btnCalc_Click procedure from Chapter 6

Figure 7-10 Gross Pay application’s interface

Passing Variables by Reference (cont.)

Page 14: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 14

Passing Variables (cont.)

Figure 7-12 Call statement entered in the procedure

Figure 7-14 Gross pay shown in the interface

Figure 7-13 CalcGross procedure

Passing Variables by Reference (cont.)

Page 15: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 15

Passing Variables (cont.)

Figure 7-15 CalcGross and btnCalc_Click procedures

Passing Variables by Reference (cont.)

Page 16: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 16

Passing Variables (cont.)

Figure 7-16 Desk-check table before the Call statement is processed

Figure 7-17 Desk-check table after the Call statement and CalcGross procedure header are processed

Passing Variables by Reference (cont.)

Page 17: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 17

Passing Variables (cont.)

Figure 7-18 Desk-check table after the statement in the CalcGross procedure is processed

Passing Variables by Reference (cont.)

Page 18: Chapter 7: Sub and Function Procedures

• Function procedure– A block of code that performs a specific task– Returns a value after completing its task– Visual Basic provides built-in functions– You can also create your own functions

• As dataType in header indicates the return type of data– The Return statement is typically the last statement in a

function• The Return expression type must agree with the As dataType

Programming with Microsoft Visual Basic 2012 18

Function Procedures

Page 19: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 19

Function Procedures (cont.)

Figure 7-22 Syntax and examples of functions

Page 20: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 20

Function Procedures (cont.)

Figure 7-23 Examples of invoking the GetNewPrice function

Page 21: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 21

Function Procedures (cont.)

Figure 7-24 CalcGross function and btnCalc_Click procedure

Page 22: Chapter 7: Sub and Function Procedures

Lesson A Summary

• There are two types of Sub procedures:– Event – Independent

• A function performs a task and returns a value• Independent procedures and functions are called from

an application’s code using the Call statement• Passing by value sends a copy of the variable’s contents

to a procedure or function • Passing by reference sends a variable’s address to a

procedure or function

Programming with Microsoft Visual Basic 2012 22

Page 23: Chapter 7: Sub and Function Procedures

Lesson B Objectives

After studying Lesson B, you should be able to:• Include a combo box in an interface• Add items to a combo box• Select a combo box item from code• Determine the number of items in the list portion of a

combo box• Sort the items in the list portion of a combo box• Determine the item either selected or entered in a

combo box• Code a combo box’s TextChanged event procedure

Programming with Microsoft Visual Basic 2012 23

Page 24: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 24

Including a Combo Box in an Interface

• Combo box– Allows the user to select from a number of choices– Allows the user to type an entry not on the list– Can save space on a form

• A list box only allows the user to select from a number of choices

• DropDownStyle property– Three values:

• Simple• DropDown (default)• DropDownList

Page 25: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 25

Including a Combo Box in an Interface (cont.)

Figure 7-27 Examples of the combo box styles

Page 26: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 26

Including a Combo Box in an Interface (cont.)

Figure 7-28 Code associated with the combo boxes in Figure 7-27

Page 27: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 27

Including a Combo Box in an Interface (cont.)

Figure 7-29 Correct TabIndex values Figure 7-30 Gross pay amount shown in the interface

Page 28: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 28

Including a Combo Box in an Interface (cont.)

Figure 7-31 Modified code for the Gross Pay application

Page 29: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 29

Lesson B Summary

• To add a combo box to a form:– Use the ComboBox tool in the toolbox

• To specify the style of a combo box:– Set the combo box’s DropDownStyle property

• To add items to a combo box:– Use the Items collection’s Add method– The method’s syntax is object.Items.Add(item)– In the syntax, object is the name of the combo box, and

item is the text you want added to the list portion of the control

Page 30: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 30

Lesson B Summary (cont.)

• To automatically sort the items in the list portion of a combo box:– Set the combo box’s Sorted property to True

• To determine the number of items in the list portion of a combo box:– Use the Items collection’s Count property– Its syntax is object.Items.Count, in which object is the

name of the combo box• To select a combo box item from code:

– Use any of the following properties: SelectedIndex, SelectedItem, or Text

Page 31: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 31

Lesson B Summary (cont.)

• To determine the item either selected in the list portion of a combo box or entered in the text portion:– Use the combo box’s Text property– However, if the combo box is a DropDownList style, you

also can use the SelectedIndex or SelectedItem property• To process code when the value in a combo box’s Text

property changes:– Enter the code in the combo box’s TextChanged event

procedure

Page 32: Chapter 7: Sub and Function Procedures

Lesson C Objectives

After studying Lesson C, you should be able to:• Prevent a form from closing• Round a number

Programming with Microsoft Visual Basic 2012 32

Page 33: Chapter 7: Sub and Function Procedures

Creating the Cerruti Company Application

Programming with Microsoft Visual Basic 2012 33

Figure 7-33 TOE chart for the Cerruti Company application

Page 34: Chapter 7: Sub and Function Procedures

Creating the Cerruti Company Application (cont.)

Programming with Microsoft Visual Basic 2012 34

Figure 7-34 User interface for the Cerruti Company application

Page 35: Chapter 7: Sub and Function Procedures

Coding the FormClosing Event Procedure

• FormClosing event– Occurs when a form is about to be closed because:

• The computer processes the Me.Close() statement • The user clicks the Close button on the form’s title bar

• Requirements for the FormClosing event procedure:– Verifying that the user wants to close the

application– Taking appropriate action based on the user’s

response• To prevent closing, set the Cancel property of the

FormClosing procedure’s e parameter to TrueProgramming with Microsoft Visual Basic 2012 35

Page 36: Chapter 7: Sub and Function Procedures

Coding the FormClosing Event Procedure (cont.)

Programming with Microsoft Visual Basic 2012 36

Figure 7-36 Message box displayed by the code in the FormClosing event procedure

Figure 7-35 Pseudocode for the FormClosing event procedure

Page 37: Chapter 7: Sub and Function Procedures

Coding the btnCalc_Click Procedure

Programming with Microsoft Visual Basic 2012 37

Figure 7-37 Pseudocode for the btnCalc_Click procedure

Page 38: Chapter 7: Sub and Function Procedures

Coding the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 38

Figure 7-38 Listing of named constants and variables for the btnCalc_Click procedure

Page 39: Chapter 7: Sub and Function Procedures

Coding the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 39

Figure 7-39 Selection structure entered in the procedure

Figure 7-40 Second selection structure entered in the procedure

Page 40: Chapter 7: Sub and Function Procedures

Creating the GetFwt Function• To calculate weekly taxable wages:

– Multiply the number of withholding allowances by $73.08 (2012 withholding allowance value)

– Subtract this result from the weekly gross pay• To determine federal withholding tax (FWT):

– Evaluate the weekly taxable wages and filing status– Use data to look up the FWT in special FWT tables

• The GetFwt function emulates the FWT table lookup

Coding the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 40

Page 41: Chapter 7: Sub and Function Procedures

Coding the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 41

Figure 7-41 Weekly FWT tables for the year 2012

Creating the GetFwt Function (cont.)

Page 42: Chapter 7: Sub and Function Procedures

Coding the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 42

Figure 7-42 Example of a FWT calculation

Figure 7-43 Another example of a FWT calculation

Creating the GetFwt Function (cont.)

Page 43: Chapter 7: Sub and Function Procedures

Coding the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 43

Figure 7-44 Pseudocode for the GetFwt function

Figure 7-45 GetFwt function header and footer

Creating the GetFwt Function (cont.)

Page 44: Chapter 7: Sub and Function Procedures

Completing the btnCalc_Click Procedure

Programming with Microsoft Visual Basic 2012 44

Rounding Numbers• You must call the GetFwt function from the btnCalc’s

Click event procedure• Math.Round function

– Rounds a value to a specific number of decimal places– Syntax: Math.Round (value[, digits])

• value is the numeric expression to work on• digits is the integer indicating the number of places to the

right of the decimal point

Page 45: Chapter 7: Sub and Function Procedures

Completing the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 45

Figure 7-46 Syntax and examples of the Math.Round function

Rounding Numbers (cont.)

Page 46: Chapter 7: Sub and Function Procedures

Completing the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 46

Figure 7-48 Payroll calculations using the first set of test data

Figure 7-47 Data for testing the Cerruti Company’s application (continues)

Rounding Numbers (cont.)

Page 47: Chapter 7: Sub and Function Procedures

Completing the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 47Figure 7-47 Data for testing the Cerruti Company’s application

Figure 7-49 Payroll calculations using the second set of test data

(continued)

Rounding Numbers (cont.)

Page 48: Chapter 7: Sub and Function Procedures

Completing the btnCalc_Click Procedure (cont.)

Programming with Microsoft Visual Basic 2012 48

Figure 7-50 Cerruti Company application’s code (continues)

Rounding Numbers (cont.)

Page 49: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 49

Figure 7-50 Cerruti Company application’s code (continues)

(continued)

Completing the btnCalc_Click Procedure (cont.)

Rounding Numbers (cont.)

Page 50: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 50

(continued)

Figure 7-50 Cerruti Company application’s code (continues)

Completing the btnCalc_Click Procedure (cont.)

Rounding Numbers (cont.)

Page 51: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 51

Figure 7-50 Cerruti Company application’s code

(continued)

Completing the btnCalc_Click Procedure (cont.)

Rounding Numbers (cont.)

Page 52: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 52

Lesson C Summary

• To process code when a form is about to be closed:– Enter the code in the form’s FormClosing event procedure– The FormClosing event occurs when the user clicks the

Close button on a form’s title bar or when the computer processes the Me.Close() statement

• To prevent a form from being closed:– Set the Cancel property of the FormClosing event

procedure’s e parameter to True, like this: e.Cancel = True

Page 53: Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2012 53

Lesson C Summary (cont.)

• To round a number to a specific number of decimal places:– Use the Math.Round function

• The function’s syntax is Math.Round(value[, digits]) – value is a numeric expression– digits (which is optional) is an integer indicating how

many places to the right of the decimal point are included in the rounding

• If the digits argument is omitted, the Math.Round function returns an integer