470 likes | 914 Views
Previewing the Harvey Industries Application. Open the Harvey Industries.exe file The Harvey Industries application calculates payroll for an employee. 2. Programming with Microsoft Visual Basic 2008, Fourth Edition. Previewing the Harvey Industries Application (continued). 3. Programming with Mi
E N D
1. Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven
Sub and Function Procedures
2. Previewing the Harvey Industries Application Open the Harvey Industries.exe file
The Harvey Industries application calculates payroll for an employee
2 Programming with Microsoft Visual Basic 2008, Fourth Edition
3. Previewing the Harvey Industries Application (continued) 3 Programming with Microsoft Visual Basic 2008, Fourth Edition
4. 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 4 Programming with Microsoft Visual Basic 2008, Fourth Edition
5. 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
6. 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 6 Programming with Microsoft Visual Basic 2008, Fourth Edition
7. 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 7 Programming with Microsoft Visual Basic 2008, Fourth Edition
8. Sub Procedures (continued) 8 Programming with Microsoft Visual Basic 2008, Fourth Edition
9. 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
9 Programming with Microsoft Visual Basic 2008, Fourth Edition
10. 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 10 Programming with Microsoft Visual Basic 2008, Fourth Edition
11. Passing a Variable by Value (continued) 11 Programming with Microsoft Visual Basic 2008, Fourth Edition
12. 12 Programming with Microsoft Visual Basic 2008, Fourth Edition Passing a Variable by Value (continued)
13. 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
13 Programming with Microsoft Visual Basic 2008, Fourth Edition
14. 14 Programming with Microsoft Visual Basic 2008, Fourth Edition Passing a Variable by Reference (continued)
15. Passing a Variable by Reference (continued) 15 Programming with Microsoft Visual Basic 2008, Fourth Edition
16. Passing a Variable by Reference (continued) 16 Programming with Microsoft Visual Basic 2008, Fourth Edition
17. Passing a Variable by Reference (continued) 17 Programming with Microsoft Visual Basic 2008, Fourth Edition
18. Passing a Variable by Reference (continued) 18 Programming with Microsoft Visual Basic 2008, Fourth Edition
19. Passing a Variable by Reference (continued) 19 Programming with Microsoft Visual Basic 2008, Fourth Edition
20. 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 20 Programming with Microsoft Visual Basic 2008, Fourth Edition
21. 21 Programming with Microsoft Visual Basic 2008, Fourth Edition Function Procedures (continued)
22. Function Procedures (continued) 22 Programming with Microsoft Visual Basic 2008, Fourth Edition
23. Function Procedures (continued) 23 Programming with Microsoft Visual Basic 2008, Fourth Edition
24. 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 24 Programming with Microsoft Visual Basic 2008, Fourth Edition
25. 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
25 Programming with Microsoft Visual Basic 2008, Fourth Edition
26. 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 26 Programming with Microsoft Visual Basic 2008, Fourth Edition
27. Including a Combo Box in an Interface (continued) 27 Programming with Microsoft Visual Basic 2008, Fourth Edition
28. 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
28 Programming with Microsoft Visual Basic 2008, Fourth Edition
29. 29 Programming with Microsoft Visual Basic 2008, Fourth Edition Including a Combo Box in an Interface (continued)
30. Including a Combo Box in an Interface (continued) 30 Programming with Microsoft Visual Basic 2008, Fourth Edition
31. 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
31 Programming with Microsoft Visual Basic 2008, Fourth Edition
32. 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 32 Programming with Microsoft Visual Basic 2008, Fourth Edition
33. Lesson C Objectives After studying Lesson C, you should be able to:
Prevent a form from closing
Round a number 33 Programming with Microsoft Visual Basic 2008, Fourth Edition
34. 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 34 Programming with Microsoft Visual Basic 2008, Fourth Edition
35. 35 Programming with Microsoft Visual Basic 2008, Fourth Edition Coding the Harvey Industries Application (continued)
36. 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
36 Programming with Microsoft Visual Basic 2008, Fourth Edition
37. Coding the FormClosing Event Procedure (continued) 37 Programming with Microsoft Visual Basic 2008, Fourth Edition
38. Coding the FormClosing Event Procedure (continued) 38 Programming with Microsoft Visual Basic 2008, Fourth Edition
39. 39 Programming with Microsoft Visual Basic 2008, Fourth Edition Coding the FormClosing Event Procedure (continued)
40. Coding the btnCalc Control’s Click Event Procedure 40 Programming with Microsoft Visual Basic 2008, Fourth Edition
41. 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 41 Programming with Microsoft Visual Basic 2008, Fourth Edition
42. Coding the GetFwt Function (continued) 42 Programming with Microsoft Visual Basic 2008, Fourth Edition
43. Coding the GetFwt Function (continued) 43 Programming with Microsoft Visual Basic 2008, Fourth Edition
44. Coding the GetFwt Function (continued) 44 Programming with Microsoft Visual Basic 2008, Fourth Edition
45. Coding the GetFwt Function (continued) 45 Programming with Microsoft Visual Basic 2008, Fourth Edition
46. 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 46 Programming with Microsoft Visual Basic 2008, Fourth Edition
47. Completing the btnCalc Control’s Click Event Procedure (continued) 47 Programming with Microsoft Visual Basic 2008, Fourth Edition
48. 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
48 Programming with Microsoft Visual Basic 2008, Fourth Edition