Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Sub and Function Procedures
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
1.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Programming with Microsoft Visual Basic 2005, Third Edition
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Arrays.
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 6: The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic th Edition
1.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 1: An Introduction to Visual Basic 2015
Chapter 3: Using Variables and Constants
Chapter 4: The Selection Structure
Repeating Program Instructions
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 2010, 5 th Edition Previewing the Harvey Industries Application (cont’d.) 3 Figure 7-1 Interface showing the payroll calculations

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 2010, 5 th Edition More About Sub Procedures Procedure ◦Block of program code that performs specific task Two types of Sub procedures in Visual Basic ◦Event procedure ◦Procedure associated with specific object and event ◦Independent Sub procedure ◦Independent of any object and event ◦Processed only when called (invoked) 5

Programming with Microsoft Visual Basic 2010, 5 th Edition Passing Variables Variables ◦Have both value and unique address 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 6

Programming with Microsoft Visual Basic 2010, 5 th Edition Passing Variables 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 7

Programming with Microsoft Visual Basic 2010, 5 th Edition Example: Event and Sub Procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 7-5 ShowMsg procedure and btnDisplay Click event procedure 9

Programming with Microsoft Visual Basic 2010, 5 th Edition Passing Variables 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 10

Programming with Microsoft Visual Basic 2010, 5 th Edition Example (Using Sub Procedures)

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 7-8 CalcGrossPay procedure and btnCalc control’s Click event procedure 12

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 13

Programming with Microsoft Visual Basic 2010, 5 th Edition 14 Figure 7-14 Function procedure syntax, examples, and steps

Programming with Microsoft Visual Basic 2010, 5 th Edition Function Procedures (cont’d.) 15 Figure 7-15 Examples of invoking the GetNewPrice function

Programming with Microsoft Visual Basic 2010, 5 th Edition Example: Using Functions

Programming with Microsoft Visual Basic 2010, 5 th Edition 17 Figure 7-16 CalcGrossPay function and btnCalc control’s Click event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Summary Two types of Sub procedures ◦Event and independent Function: Performs task and returns value Independent procedures and functions ◦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 18

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 19

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 20

Programming with Microsoft Visual Basic 2010, 5 th Edition Including a Combo Box in an Interface (cont’d.) 21 Figure 7-19 Examples of the combo box styles

Programming with Microsoft Visual Basic 2010, 5 th Edition 22 Figure 7-20 Code associated with the combo boxes in Figure 7-19

Programming with Microsoft Visual Basic 2010, 5 th Edition Including a Combo Box in an Interface (cont’d.) To process code when Text property changes ◦Use TextChanged event procedure 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: Contains value of item selected ◦Text: Contains value that appears in text portion Use label control to provide keyboard access to the combo box 23

Programming with Microsoft Visual Basic 2010, 5 th Edition Including a Combo Box in an Interface (cont’d.) 24 Figure 7-22 Gross pay amount shown in the interface

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary Use ComboBox tool to add combo box to form 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 Enter code in combo box’s TextChanged event procedure ◦To process code when Text property changes 25

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary (cont’d.) Use SelectedIndex, SelectedItem, or Text property: ◦To select combo box item from code ◦To determine item that was selected 26

Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Objectives After studying Lesson C, you should be able to: Prevent a form from closing Round a number 27

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the Harvey Industries Application Application objective is to calculate: ◦Employee’s weekly gross pay ◦Federal withholding tax (FWT) ◦Social Security and Medicare (FICA) tax ◦Net pay Review TOE chart for requirements 28

Programming with Microsoft Visual Basic 2010, 5 th Edition Figure 7-26 User interface for the Harvey Industries application Coding the Harvey Industries Application (cont’d.) 29

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 30

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the FormClosing Event Procedure (cont’d.) 31 Figure 7-27 Pseudocode for the FormClosing event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the FormClosing Event Procedure (cont’d.) 32 Figure 7-28 Message box displayed by the code in the FormClosing event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the btnCalc Control’s Click Event Procedure 33 Figure 7-29 Pseudocode for the btnCalc control’s Click event procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the btnCalc Control’s Click Event Procedure (cont’d.) 34 Figure 7-30 Selection structure entered in the procedure

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the GetFwt Function How to calculate weekly taxable wages ◦Multiply number of withholding allowances by $70.19 ◦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 35

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the GetFwt Function (cont’d.) 36 Figure 7-33 FWT calculations for a married taxpayer whose weekly taxable wages are $388.46

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the GetFwt Function (cont’d.) 37 Figure 7-34 FWT calculations for a single taxpayer whose weekly taxable wages are $600

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the GetFwt Function (cont’d.) 38 Figure 7-35 Pseudocode for the GetFwt function

Programming with Microsoft Visual Basic 2010, 5 th Edition Creating the GetFwt Function (cont’d.) 39 Figure 7-36 GetFwt function header and footer

Programming with Microsoft Visual Basic 2010, 5 th Edition Completing the btnCalc Control’s Click Event Procedure Must call GetFwt function from btnCalc’s Click event procedure Math.Round function ◦Round value to specific number of decimal places ◦Syntax: Math.Round (value[, digits]) ◦value: Numeric expression to work on ◦digits: Integer indicating number of places to right of decimal point 40

Programming with Microsoft Visual Basic 2010, 5 th Edition Completing the btnCalc Control’s Click Event Procedure (cont’d.) 41 Figure 7-37 Payroll calculations displayed in the interface

Programming with Microsoft Visual Basic 2010, 5 th Edition 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 42