Tut 7 - PSUT

advertisement
Chapter 7: Sub and Function
Procedures
Programming with Microsoft Visual Basic
.NET, Second Edition
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
Programming with Microsoft Visual Basic .NET, Second Edition
2
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
Programming with Microsoft Visual Basic .NET, Second Edition
3
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
Programming with Microsoft Visual Basic .NET, Second Edition
4
Sub Procedures (continued)
• User-defined procedures
– You must call explicitly
– You can define parameters
Programming with Microsoft Visual Basic .NET, Second Edition
5
Including Parameters in a UserDefined Sub Procedure
Figure 7-2: Syntax for creating a user-defined Sub procedure
Programming with Microsoft Visual Basic .NET, Second Edition
6
Including Parameters in a UserDefined 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
Programming with Microsoft Visual Basic .NET, Second Edition
7
Including Parameters in a UserDefined 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
Programming with Microsoft Visual Basic .NET, Second Edition
8
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
Programming with Microsoft Visual Basic .NET, Second Edition
9
Passing Variables (continued)
Figure 7-3: Examples of passing variables by value
Programming with Microsoft Visual Basic .NET, Second Edition
10
Passing Variables (continued)
Figure 7-3: Examples of passing variables by value (continued)
Programming with Microsoft Visual Basic .NET, Second Edition
11
Passing Variables (continued)
Figure 7-4: Examples of passing variables by reference
Programming with Microsoft Visual Basic .NET, Second Edition
12
Passing Variables (continued)
Figure 7-4: Examples of passing variables by reference
(continued)
Programming with Microsoft Visual Basic .NET, Second Edition
13
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
Programming with Microsoft Visual Basic .NET, Second Edition
14
Function Procedures (continued)
Figure 7-6: User-defined function syntax and example
Programming with Microsoft Visual Basic .NET, Second Edition
15
Using a List Box Control
Lesson 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
Programming with Microsoft Visual Basic .NET, Second Edition
16
Using a List Box Control
Lesson B Objectives (continued)
• Determine the selected item in a list box
• Round a number
• Code a list box’s SelectedValueChanged event
Programming with Microsoft Visual Basic .NET, Second Edition
17
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
Programming with Microsoft Visual Basic .NET, Second Edition
18
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
Programming with Microsoft Visual Basic .NET, Second Edition
19
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
Programming with Microsoft Visual Basic .NET, Second Edition
20
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
Programming with Microsoft Visual Basic .NET, Second Edition
21
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
Programming with Microsoft Visual Basic .NET, Second Edition
22
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
Programming with Microsoft Visual Basic .NET, Second Edition
23
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
Programming with Microsoft Visual Basic .NET, Second Edition
24
Coding the uiCalculateButton Click
Event Procedure
Figure 7-18: Pseudocode for the uiCalculateButton control’s
Click event procedure
Programming with Microsoft Visual Basic .NET, Second Edition
25
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
Programming with Microsoft Visual Basic .NET, Second Edition
26
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
Programming with Microsoft Visual Basic .NET, Second Edition
27
Coding the GetFwtTax Function
(continued)
Figure 7-23: Pseudocode for the GetFwtTax function
Programming with Microsoft Visual Basic .NET, Second Edition
28
Coding the GetFwtTax Function
(continued)
Figure 7-23: Pseudocode for the GetFwtTax function (continued)
Programming with Microsoft Visual Basic .NET, Second Edition
29
Completing the uiCalculateButton’s
Click Event Procedure
• You can call the GetFwtTax function from the
uiCalculateButton’s Click event procedure
Programming with Microsoft Visual Basic .NET, Second Edition
30
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
Programming with Microsoft Visual Basic .NET, Second Edition
31
Completing the Payroll Application
Lesson 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
Programming with Microsoft Visual Basic .NET, Second Edition
32
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
Programming with Microsoft Visual Basic .NET, Second Edition
33
Adding an Existing Form to a
Solution (continued)
Figure 7-30: Copyright form
Programming with Microsoft Visual Basic .NET, Second Edition
34
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
Programming with Microsoft Visual Basic .NET, Second Edition
35
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
Programming with Microsoft Visual Basic .NET, Second Edition
36
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
Programming with Microsoft Visual Basic .NET, Second Edition
37
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
Programming with Microsoft Visual Basic .NET, Second Edition
38
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
Programming with Microsoft Visual Basic .NET, Second Edition
39
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()
Programming with Microsoft Visual Basic .NET, Second Edition
40
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
Programming with Microsoft Visual Basic .NET, Second Edition
41
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
Programming with Microsoft Visual Basic .NET, Second Edition
42
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()
Programming with Microsoft Visual Basic .NET, Second Edition
43
Download