1 / 48

Programming with Microsoft Visual Basic 2008 Fourth Edition

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.

quynh
Download Presentation

Programming with Microsoft Visual Basic 2008 Fourth Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  3. Previewing the Harvey Industries Application (continued) Figure 7-1: Payroll calculations shown in the interface 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 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

  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 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  8. Sub Procedures (continued) Figure 7-2: Syntax of an independent Sub procedure and the Call statement 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 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  11. Passing a Variable by Value (continued) Figure 7-4: Additional lines of code entered in the ShowMsg procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

  12. Passing a Variable by Value (continued) Figure 7-5: ShowMsg procedure and btnDisplay Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

  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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  14. Passing a Variable by Reference (continued) Figure 7-8: CalcGrossPay procedure and btnCalc contro’s Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

  15. Passing a Variable by Reference (continued) Figure 7-9: Desk-check table before the computer processes the Call statement Programming with Microsoft Visual Basic 2008, Fourth Edition

  16. Passing a Variable by Reference (continued) Figure 7-10: Desk-check table after the computer processes the Call statement and the CalcGrossPay procedure header Programming with Microsoft Visual Basic 2008, Fourth Edition

  17. Passing a Variable by Reference (continued) Figure 7-11: Desk-check table after the computer processes the first statement in the CalcGrossPay procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

  18. 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  19. Passing a Variable by Reference (continued) Figure 7-13: Desk-check table after the CalcGrossPay procedure ends 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 • Returnexpression type must agree with As datatype Programming with Microsoft Visual Basic 2008, Fourth Edition

  21. Function Procedures (continued) Figure 7-14: Syntax, example, and steps for creating a function Programming with Microsoft Visual Basic 2008, Fourth Edition

  22. Function Procedures (continued) Figure 7-15: Examples of invoking the GetNewPrice function Programming with Microsoft Visual Basic 2008, Fourth Edition

  23. Function Procedures (continued) Figure 7-16: CalcGrossPay function and btnCalc control’s Click event procedure 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 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 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  27. Including a Combo Box in an Interface (continued) Figure 7-19: Examples of the combo box styles 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  29. Including a Combo Box in an Interface (continued) Figure 7-20: Code used to add items to the combo boxes and also select a default item Programming with Microsoft Visual Basic 2008, Fourth Edition

  30. Including a Combo Box in an Interface (continued) Figure 7-22: Interface showing the gross pay 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 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 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 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  35. Coding the Harvey Industries Application (continued) Figure 7-25: User interface for the Harvey Industries application Programming with Microsoft Visual Basic 2008, Fourth Edition

  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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  37. Coding the FormClosing Event Procedure (continued) Figure 26: Pseudocode for the FormClosing event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

  38. Coding the FormClosing Event Procedure (continued) Figure 27: Message box displayed by the FormClosing event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

  39. Coding the FormClosing Event Procedure (continued) Figure 28: Pseudocode for the btnCalc control’s Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

  40. Coding the btnCalc Control’s Click Event Procedure Figure 29: Selection structure entered in the procedure 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  42. Coding the GetFwt Function (continued) Figure 7-31: FWT calculations for a married taxpayer with taxable wages of $288.46 Programming with Microsoft Visual Basic 2008, Fourth Edition

  43. Coding the GetFwt Function (continued) Figure 7-32: FWT calculations for a single taxpayer with taxable wages of $600.00 Programming with Microsoft Visual Basic 2008, Fourth Edition

  44. Coding the GetFwt Function (continued) Figure 7-33: Pseudocode for the GetFwt function Programming with Microsoft Visual Basic 2008, Fourth Edition

  45. Coding the GetFwt Function (continued) Figure 7-34: GetFWT function header and footer 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

  47. Completing the btnCalc Control’s Click Event Procedure (continued) Figure 7-35: Payroll calculations displayed in the interface 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 Programming with Microsoft Visual Basic 2008, Fourth Edition

More Related