Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function...

48
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures

Transcript of Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function...

Page 1: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Programming with Microsoft Visual Basic 2008

Fourth Edition

Chapter Seven

Sub and Function Procedures

Page 2: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Previewing the Harvey Industries Application

• Open the Harvey Industries.exe file

• The Harvey Industries application calculates payroll for an employee

2Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 3: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Previewing the Harvey Industries Application (continued)

3Programming with Microsoft Visual Basic 2008, Fourth Edition

Figure 7-1: Payroll calculations shown in the interface

Page 4: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Lesson A Objectives

After studying Lesson A, you should be able to:

• 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

4Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 5: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Procedures

• Procedure:– Block of program code that performs specific task

• Two types of procedures in Visual Basic:– Sub procedure: Does not return value– Function procedure: Does return value

Programming with Microsoft Visual Basic 2008, Fourth Edition 5

Page 6: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Sub Procedures

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

• Event procedure: Associated with specific object and event– Processed automatically in response to event

• Independent Sub procedure: Independent of any object and event– Invoked from code using Call statement

• Parameter: Data passed to procedure when invoked

6Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 7: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Sub Procedures (continued)

• Call statement: Used to invoke procedure• Syntax: Call procedurename([argumentlist])

– argumentlist: Used to pass information (optional)

• Argument: Data item in argumentlist

• Parameter: Data item in parameterlist

• Relationship between arguments and parameters– Should agree in number, position, and data type

• Types of data that can be passed as arguments to a procedure:– Variable, literal constant, named constant, keyword

7Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 8: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Sub Procedures (continued)

Figure 7-2: Syntax of an independent Sub procedure and the Call statement

8Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 9: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing Variables

• A variable has value and unique address in memory

• You can pass either variable’s value or its address to procedure

• Passing by value:– Passes copy of value stored in variable

• Passing by reference: – Passes memory address of variable– Allows procedure to change contents of variable

9Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 10: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Value

• Passing by value: Provides only contents of variable to receiving procedure

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

• Reasons to pass by value:– Procedure needs to know contents of variable– Procedure does not need to change original value

• By default, Visual Basic passes by value

10Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 11: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Value (continued)

Figure 7-4: Additional lines of code entered in the ShowMsg procedure

11Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 12: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Figure 7-5: ShowMsg procedure and btnDisplay Click event procedure

12Programming with Microsoft Visual Basic 2008, Fourth Edition

Passing a Variable by Value (continued)

Page 13: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Reference

• Passing by reference: Provides address (memory location) of variable to procedure– Receiving procedure can thus access variable

• Reason to pass by reference:– Procedure needs to change variable’s contents

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

13Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 14: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Figure 7-8: CalcGrossPay procedure and btnCalc contro’s Click event procedure

14Programming with Microsoft Visual Basic 2008, Fourth Edition

Passing a Variable by Reference (continued)

Page 15: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Reference (continued)

Figure 7-9: Desk-check table before the computer processes the Call statement

15Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 16: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Reference (continued)

Figure 7-10: Desk-check table after the computer processes the Call statement and the CalcGrossPay procedure header

16Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 17: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Reference (continued)

Figure 7-11: Desk-check table after the computer processes the first statement in the CalcGrossPay procedure

17Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 18: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Reference (continued)

Figure 7-12: Desk-check table after the computer processes the statement in the selection structure’s true path

18Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 19: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Passing a Variable by Reference (continued)

Figure 7-13: Desk-check table after the CalcGrossPay procedure ends

19Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 20: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Function Procedures

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

• Visual Basic provides built-in functions

• Can also create your own functions– As datatype in header indicates return type of data– Return expression type must agree with As

datatype

20Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 21: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Figure 7-14: Syntax, example, and steps for creating a function

21Programming with Microsoft Visual Basic 2008, Fourth Edition

Function Procedures (continued)

Page 22: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Function Procedures (continued)

Figure 7-15: Examples of invoking the GetNewPrice function

22Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 23: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Function Procedures (continued)

Figure 7-16: CalcGrossPay function and btnCalc control’s Click event procedure

23Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 24: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Lesson A Summary

• Two types of procedures: Event and independent

• Function: Performs task and returns value

• Independent procedures and functions are called from application’s code using Call statement

• Pass by value: Send copy of variable’s contents to procedure or function

• Pass by reference: Send variable’s address to procedure or function

24Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 25: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven 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 item either selected or entered in a combo box

• Code a combo box’s TextChanged event procedure

25Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 26: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Including a Combo Box in an Interface

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

• List box does not share features two and three

• DropDownStyle property:– Values: Simple, DropDown (default),

DropDownList

26Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 27: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Including a Combo Box in an Interface (continued)

Figure 7-19: Examples of the combo box styles

27Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 28: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Including a Combo Box in an Interface (continued)

• Change in item value causes TextChanged event

• Use Item collection’s Add method to add item

• Other properties of combo box– Sorted: Sorts items in dictionary order– SelectedIndex: Used to select item in list portion– SelectedItem: Determines which item is selected– Text: Used to get or set value in text portion

28Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 29: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Figure 7-20: Code used to add items to the combo boxes and also select a default item

29Programming with Microsoft Visual Basic 2008, Fourth Edition

Including a Combo Box in an Interface (continued)

Page 30: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Including a Combo Box in an Interface (continued)

Figure 7-22: Interface showing the gross pay

30Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 31: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Lesson B Summary

• Combo box displays list of items for selection

• Combo box allows user to type entry not on list

• Specify style of combo box using DropDownStyle property

• Use Items collection’s Add method to add items to Combo box

• Use combo box’s Sorted property to sort items in combo’s list

31Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 32: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Lesson B Summary (continued)

• Use SelectedIndex, SelectedItem, or Text property to select combo box item from code

• Use SelectedIndex, SelectedItem, or Text property to determine item that was selected

32Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 33: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Lesson C Objectives

After studying Lesson C, you should be able to:

• Prevent a form from closing

• Round a number

33Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 34: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the Harvey Industries Application

• Objective: – Calculate employee’s weekly gross pay, federal

withholding tax (FWT), Social Security and Medicare (FICA) tax, and net pay

• Review TOE chart for requirements

34Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 35: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Figure 7-25: User interface for the Harvey Industries application

35Programming with Microsoft Visual Basic 2008, Fourth Edition

Coding the Harvey Industries Application (continued)

Page 36: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the FormClosing Event Procedure

• FormClosing event: Occurs when form is about to be closed because:– Computer processes Me.Close() statement – User clicks Close button on form’s title bar

• Requirement for FormClosing event procedure: – Verifying that user wants to close application– Taking appropriate action based on user’s

response

• To prevent closing, set Cancel property of FormClosing procedure’s e parameter to true

36Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 37: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the FormClosing Event Procedure (continued)

Figure 26: Pseudocode for the FormClosing event procedure

37Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 38: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the FormClosing Event Procedure (continued)

Figure 27: Message box displayed by the FormClosing event procedure

38Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 39: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

39Programming with Microsoft Visual Basic 2008, Fourth Edition

Figure 28: Pseudocode for the btnCalc control’s Click event procedure

Coding the FormClosing Event Procedure (continued)

Page 40: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the btnCalc Control’s Click Event Procedure

Figure 29: Selection structure entered in the procedure

40Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 41: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the GetFwt Function

• How to calculate weekly taxable wages:– Multiply number of withholding allowances by

$67.31– Subtract this result from weekly gross pay

• Determining federal withholding tax (FWT): – Evaluate weekly taxable wages and filing status– Use data to look up FWT in special FWT tables

• GetFwt function emulates FWT table lookup

41Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 42: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the GetFwt Function (continued)

Figure 7-31: FWT calculations for a married taxpayer with taxable wages of $288.46

42Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 43: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the GetFwt Function (continued)

43Programming with Microsoft Visual Basic 2008, Fourth Edition

Figure 7-32: FWT calculations for a single taxpayer with taxable wages of $600.00

Page 44: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the GetFwt Function (continued)

Figure 7-33: Pseudocode for the GetFwt function

44Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 45: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Coding the GetFwt Function (continued)

45Programming with Microsoft Visual Basic 2008, Fourth Edition

Figure 7-34: GetFWT function header and footer

Page 46: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Completing the btnCalc Control’s Click Event Procedure

• Must call GetFwt function from btnCalc’s Click event procedure

• Math.Round function: Used to round value to specific number of decimal places

• Syntax: Math.Round (value[, digits])– value: Numeric value to work on– digits: Number of places to right of decimal point

46Programming with Microsoft Visual Basic 2008, Fourth Edition

Page 47: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Completing the btnCalc Control’s Click Event Procedure (continued)

47Programming with Microsoft Visual Basic 2008, Fourth Edition

Figure 7-35: Payroll calculations displayed in the interface

Page 48: Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.

Lesson C Summary

• Use form’s FormClosing event procedure to process code when form is about to be closed

• Set Cancel property of FormClosing event procedure’s e parameter to true to prevent form from being closed

• Use Math.Round function to round number to specific number of decimal places

48Programming with Microsoft Visual Basic 2008, Fourth Edition