Programming with Microsoft Visual Basic 2012 Chapter 7: 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
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
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.
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.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
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 th Edition CHAPTER SEVEN 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.
Chapter Four The Selection Structure
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
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
Chapter 12: How Long Can This Go On?
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 10: Structures and Sequential Access Files
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.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6: The Repetition Structure
Tutorial 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.
Chapter 5: More on the Selection Structure
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.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
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.
Chapter 11: Testing, Testing…1, 2, 3
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
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

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

Previewing the Cerruti Company Application Programming with Microsoft Visual Basic Figure 7-1 Interface showing the payroll calculations

Previewing the Cerruti Company Application (cont.) Programming with Microsoft Visual Basic Figure 7-2 Message box containing a confirmation message

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 20124

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 Sub Procedures

Programming with Microsoft Visual Basic Sub Procedures (cont.) Figure 7-3 Syntax and examples of an independent Sub procedure and the Call statement

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 Passing Variables Figure 7-4 Illustrations of passing by value and passing by reference

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic 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.)

Programming with Microsoft Visual Basic Passing Variables (cont.) Figure 7-8 Message shown in the interface Figure 7-7 DisplayMsg procedure Passing Variables by Value (cont.)

Programming with Microsoft Visual Basic Passing Variables (cont.) Figure 7-9 DisplayMsg procedure header and Call statement Passing Variables by Value (cont.)

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic 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.)

Programming with Microsoft Visual Basic 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.)

Programming with Microsoft Visual Basic Passing Variables (cont.) Figure 7-15 CalcGross and btnCalc_Click procedures Passing Variables by Reference (cont.)

Programming with Microsoft Visual Basic 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.)

Programming with Microsoft Visual Basic Passing Variables (cont.) Figure 7-18 Desk-check table after the statement in the CalcGross procedure is processed Passing Variables by Reference (cont.)

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 Function Procedures

Programming with Microsoft Visual Basic Function Procedures (cont.) Figure 7-22 Syntax and examples of functions

Programming with Microsoft Visual Basic Function Procedures (cont.) Figure 7-23 Examples of invoking the GetNewPrice function

Programming with Microsoft Visual Basic Function Procedures (cont.) Figure 7-24 CalcGross function and btnCalc_Click procedure

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

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

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic Including a Combo Box in an Interface (cont.) Figure 7-27 Examples of the combo box styles

Programming with Microsoft Visual Basic Including a Combo Box in an Interface (cont.) Figure 7-28 Code associated with the combo boxes in Figure 7-27

Programming with Microsoft Visual Basic Including a Combo Box in an Interface (cont.) Figure 7-29 Correct TabIndex values Figure 7-30 Gross pay amount shown in the interface

Programming with Microsoft Visual Basic Including a Combo Box in an Interface (cont.) Figure 7-31 Modified code for the Gross Pay application

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic 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

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

Creating the Cerruti Company Application Programming with Microsoft Visual Basic Figure 7-33 TOE chart for the Cerruti Company application

Creating the Cerruti Company Application (cont.) Programming with Microsoft Visual Basic Figure 7-34 User interface for the Cerruti Company application

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 True Programming with Microsoft Visual Basic

Coding the FormClosing Event Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-36 Message box displayed by the code in the FormClosing event procedure Figure 7-35 Pseudocode for the FormClosing event procedure

Coding the btnCalc_Click Procedure Programming with Microsoft Visual Basic Figure 7-37 Pseudocode for the btnCalc_Click procedure

Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-38 Listing of named constants and variables for the btnCalc_Click procedure

Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-39 Selection structure entered in the procedure Figure 7-40 Second selection structure entered in the procedure

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

Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-41 Weekly FWT tables for the year 2012 Creating the GetFwt Function (cont.)

Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-42 Example of a FWT calculation Figure 7-43 Another example of a FWT calculation Creating the GetFwt Function (cont.)

Coding the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-44 Pseudocode for the GetFwt function Figure 7-45 GetFwt function header and footer Creating the GetFwt Function (cont.)

Completing the btnCalc_Click Procedure Programming with Microsoft Visual Basic 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

Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-46 Syntax and examples of the Math.Round function Rounding Numbers (cont.)

Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic 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.)

Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 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.)

Completing the btnCalc_Click Procedure (cont.) Programming with Microsoft Visual Basic Figure 7-50 Cerruti Company application’s code (continues) Rounding Numbers (cont.)

Programming with Microsoft Visual Basic Figure 7-50 Cerruti Company application’s code (continues) (continued) Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.)

Programming with Microsoft Visual Basic (continued) Figure 7-50 Cerruti Company application’s code (continues) Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.)

Programming with Microsoft Visual Basic Figure 7-50 Cerruti Company application’s code (continued) Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.)

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic 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