chapter 7: sub and function procedures programming with microsoft visual basic.net, second edition

43
Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic .NET, Second Edition

Upload: jonathan-bruce

Post on 16-Dec-2015

233 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic .NET, Second Edition

Page 2: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 2

Creating Sub and Function Procedures

Lesson A Objectives• 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 by reference

• Create a Function procedure

Page 3: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 3

Procedures

• A procedure is a block of program code that performs a specific task

• Procedures in Visual Basic .NET can be either Sub procedures or Function procedures

– Sub procedures do not return a value

– Function procedures return a value after performing their assigned task

Page 4: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 4

Sub Procedures

• Event procedures

– Called by Visual Basic .NET in response to an event

– Every event procedure has at least two parameters

• sender: the object that raised the event

• e: information about the object

Page 5: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 5

Sub Procedures (continued)

• User-defined procedures

– You must call explicitly

– You can define parameters

Page 6: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 6

Including Parameters in a User-Defined Sub Procedure

Figure 7-2: Syntax for creating a user-defined Sub procedure

Page 7: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 7

Including Parameters in a User-Defined Sub Procedure (continued)

• User-defined Sub procedures have both a procedure header and procedure footer

• You use the keyword Private to indicate that only the procedures in the current form can access the procedure

• You use the keyword Public when you want to allow unlimited access to the procedure

• The Sub keyword identifies the procedure as a Sub procedure

Page 8: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 8

Including Parameters in a User-Defined Sub Procedure (continued)

• parameterlist

– Lists the data type and name of memory locations used by the procedure to store the information passed to it

– Specifies how each item of information is passed: either by value or by reference

• The procedure footer for a Sub procedure is always End Sub

Page 9: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 9

Passing Variables

• Passing by value: makes a copy of the data

– Use ByVal before the parameter name

– This is the default if you do not specify

• Passing by reference: passes the address of the data

– Use ByRef before the parameter name

Page 10: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 10

Passing Variables (continued)

Figure 7-3: Examples of passing variables by value

Page 11: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 11

Passing Variables (continued)

Figure 7-3: Examples of passing variables by value (continued)

Page 12: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 12

Passing Variables (continued)

Figure 7-4: Examples of passing variables by reference

Page 13: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 13

Passing Variables (continued)

Figure 7-4: Examples of passing variables by reference (continued)

Page 14: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 14

Function Procedures

• Function procedure

– A block of code that performs a specific task

– Typically referred to as a function

• You can create your own functions, referred to as user-defined functions

• The Return statement alerts the computer that the function has completed its task and ends the function after returning the value of its expression

Page 15: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 15

Function Procedures (continued)

Figure 7-6: User-defined function syntax and example

Page 16: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 16

Using a List Box ControlLesson B Objectives

• Add a list box to a form

• Add items to a list box

• Sort the contents of a list box

• Select a list box item from code

Page 17: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 17

Using a List Box ControlLesson B Objectives (continued)

• Determine the selected item in a list box

• Round a number

• Code a list box’s SelectedValueChanged event

Page 18: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 18

Completing the Payroll Application’s User Interface

• The payroll application should allow Mr. Williams to enter an employee’s:

– Name

– Hours worked

– Rate of pay

– Marital status (either “Married” or “Single”)

– Number of withholding allowances

Page 19: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 19

Completing the Payroll Application’s User Interface (continued)

• The application should calculate the employee’s:

– Weekly gross pay

– Federal withholding tax (FWT)

– Social Security and Medicare (FICA) tax

– Net pay

Page 20: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 20

Adding a List Box to a Form

• Use a list box control to display a list of choices from which the user can select zero choices, one choice, or more than one choice

• The number of choices the user is allowed to select is controlled by the list box control’s SelectionMode property

• The Windows standard for list boxes is to display a minimum of three selections and a maximum of eight selections at a time

Page 21: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 21

Adding Items to a List Box

• The items in a list box belong to a collection called the Items collection

– The first item in the Items collection appears as the first item in the list box

– The second item in the Items collection appears as the second item in the list box, and so on

– The first item has an index of zero

– The second item has an index of one, and so on

Page 22: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 22

Adding Items to a List Box (continued)

• Use the Items collection’s Add method to specify the items you want displayed in a list box control

• When you use the Add method to add an item to a list box, the position of the item in the list depends on the value stored in the list box’s Sorted property

Page 23: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 23

The SelectedItem and SelectedIndex Properties

• A list box’s SelectItem property and SelectedIndex property can be used both to:

– Determine the item selected in the list box

– Select a list box item from code

Page 24: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 24

The SelectedItem and SelectedIndex Properties

(continued)• The selected item is also called the default list

box item

– Should be either the most used selection

– Or, if all of the selections are used fairly equally, the first selection in the list

Page 25: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 25

Coding the uiCalculateButton Click Event Procedure

Figure 7-18: Pseudocode for the uiCalculateButton control’s Click event procedure

Page 26: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 26

Coding the GetFwtTax Function

• The amount of federal withholding text (FWT) to deduct from an employee’s weekly gross pay is based on the employee’s:

– Filing status: single (including head of household) or married

– Weekly taxable wages

Page 27: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 27

Coding the GetFwtTax Function (continued)

• To calculate the federal withholding tax you need to know the employee’s:

– Gross pay amount

– Marital status

– Number of withholding allowances

Page 28: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 28

Coding the GetFwtTax Function (continued)

Figure 7-23: Pseudocode for the GetFwtTax function

Page 29: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 29

Coding the GetFwtTax Function (continued)

Figure 7-23: Pseudocode for the GetFwtTax function (continued)

Page 30: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 30

Completing the uiCalculateButton’s Click Event Procedure

• You can call the GetFwtTax function from the uiCalculateButton’s Click event procedure

Page 31: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 31

Clearing the Contents of the Label Controls

• The label controls should be cleared when the SelectedValueChanged event occurs for one of the list boxes in the interface

• A list box’s SelectedValueChanged event occurs each time a different value is selected in the list box

Page 32: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 32

Completing the Payroll ApplicationLesson C Objectives

• Add an existing form to a solution

• Add a new module to a solution

• Code the Sub Main procedure

• Create an instance of a form

• Display a form object using the ShowDialog method

Page 33: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 33

Adding an Existing Form to a Solution

• The copyright screen for Interlocking Software Company is to be the splash screen for each custom application created by the company

• The copyright screen identifies the application’s author and copyright year and includes the Interlocking Software Company logo

Page 34: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 34

Adding an Existing Form to a Solution (continued)

Figure 7-30: Copyright form

Page 35: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 35

Coding the Sub Main Procedure

• Sub Main is a special procedure in Visual Basic .NET that can be declared as the “starting point” for an application

• You can tell the computer to process the Sub Main procedure automatically when an application is started

Page 36: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 36

Coding the Sub Main Procedure (continued)

• You enter the Sub Main procedure in a module

• A module is a file that contains code that is not associated with any specific object in the interface

Page 37: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 37

Creating an Instance of a Form

• A class definition (or class) specifies the attributes and behaviors of an object

• When an application is started, Visual Basic .NET automatically processes the code contained in the Startup object

• If the Sub Main procedure is the Startup object, neither the CopyrightForm class definition nor the PayrollForm class definition will be processed automatically

Page 38: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 38

Creating an Instance of a Form (continued)

• To allow the payroll application to display the forms associated with the CopyrightForm and the PayrollForm classes, the Sub Main procedure will need to:

– Instruct the computer to create the objects

– Instruct the computer to display the objects on the screen

Page 39: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 39

Creating an Instance of a Form (continued)

• Syntax used to instruct the computer to create an object from a class: Dim variablename As New classname

Page 40: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 40

Using a Form Object’s Show Dialog Method

• The form object’s ShowDialog method allows you to display a form object on the screen

• The syntax of the ShowDialog method is: form.ShowDialog()

Page 41: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 41

Summary

• To pass a variable by value to a procedure, include the keyword ByVal before the parameter name in the procedure header’s parameterlist

• To pass a variable by reference, include the keyword ByRef before the parameter name in the procedure header’s parameterlist

• To add items to a list box, use the Items collection’s Add method

Page 42: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 42

Summary (continued)

• To determine the item selected in a list box, or to select a list box item from code, set the list box’s SelectedItem property or its SelectedIndex property

• To process code when a different value is selected in a list box, enter the code in either the list box’s SelectedValueChanged or SelectedIndexChanged event procedure

Page 43: Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 43

Summary (continued)

• To add an existing item to an application, click File on the menu bar then click Add Existing Item

• To create an instance of an object from a class, use the syntax: Dim variablename As New classname

• To display a form object on the screen, use the form object’s ShowDialog method, whose syntax is: form.ShowDialog()