Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures
Jan 22, 2016
Programming with Microsoft Visual Basic 2012
Chapter 7: Sub and Function Procedures
Previewing the Cerruti Company Application
Programming with Microsoft Visual Basic 2012 2
Figure 7-1 Interface showing the payroll calculations
Previewing the Cerruti Company Application (cont.)
Programming with Microsoft Visual Basic 2012 3
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 2012 4
• 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 2012 5
Sub Procedures
Programming with Microsoft Visual Basic 2012 6
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 2012 7
Passing Variables
Figure 7-4 Illustrations of passing by value and passing by reference
Programming with Microsoft Visual Basic 2012 8
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 2012 9
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 2012 10
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 2012 11
Passing Variables (cont.)
Figure 7-9 DisplayMsg procedure header and Call statement
Passing Variables by Value (cont.)
Programming with Microsoft Visual Basic 2012 12
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 2012 13
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 2012 14
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 2012 15
Passing Variables (cont.)
Figure 7-15 CalcGross and btnCalc_Click procedures
Passing Variables by Reference (cont.)
Programming with Microsoft Visual Basic 2012 16
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 2012 17
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 2012 18
Function Procedures
Programming with Microsoft Visual Basic 2012 19
Function Procedures (cont.)
Figure 7-22 Syntax and examples of functions
Programming with Microsoft Visual Basic 2012 20
Function Procedures (cont.)
Figure 7-23 Examples of invoking the GetNewPrice function
Programming with Microsoft Visual Basic 2012 21
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 2012 22
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 2012 23
Programming with Microsoft Visual Basic 2012 24
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 2012 25
Including a Combo Box in an Interface (cont.)
Figure 7-27 Examples of the combo box styles
Programming with Microsoft Visual Basic 2012 26
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 2012 27
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 2012 28
Including a Combo Box in an Interface (cont.)
Figure 7-31 Modified code for the Gross Pay application
Programming with Microsoft Visual Basic 2012 29
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 2012 30
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 2012 31
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 2012 32
Creating the Cerruti Company Application
Programming with Microsoft Visual Basic 2012 33
Figure 7-33 TOE chart for the Cerruti Company application
Creating the Cerruti Company Application (cont.)
Programming with Microsoft Visual Basic 2012 34
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 TrueProgramming with Microsoft Visual Basic 2012 35
Coding the FormClosing Event Procedure (cont.)
Programming with Microsoft Visual Basic 2012 36
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 2012 37
Figure 7-37 Pseudocode for the btnCalc_Click procedure
Coding the btnCalc_Click Procedure (cont.)
Programming with Microsoft Visual Basic 2012 38
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 2012 39
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 2012 40
Coding the btnCalc_Click Procedure (cont.)
Programming with Microsoft Visual Basic 2012 41
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 2012 42
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 2012 43
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 2012 44
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 2012 45
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 2012 46
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 2012 47Figure 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 2012 48
Figure 7-50 Cerruti Company application’s code (continues)
Rounding Numbers (cont.)
Programming with Microsoft Visual Basic 2012 49
Figure 7-50 Cerruti Company application’s code (continues)
(continued)
Completing the btnCalc_Click Procedure (cont.)
Rounding Numbers (cont.)
Programming with Microsoft Visual Basic 2012 50
(continued)
Figure 7-50 Cerruti Company application’s code (continues)
Completing the btnCalc_Click Procedure (cont.)
Rounding Numbers (cont.)
Programming with Microsoft Visual Basic 2012 51
Figure 7-50 Cerruti Company application’s code
(continued)
Completing the btnCalc_Click Procedure (cont.)
Rounding Numbers (cont.)
Programming with Microsoft Visual Basic 2012 52
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 2012 53
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