22
Tutorial 7: Sub and Function Pro cedures 1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Embed Size (px)

Citation preview

Page 1: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 1

Tutorial 7Sub and Function Procedures

Page 2: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 2

Creating Sub and Function ProceduresLesson A Objectives

After completing this lesson, you will 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 by reference

Create a Function procedure

Page 3: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 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

Function procedures return a value after performing their assigned task

Sub procedures do not return a value

Page 4: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 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

User-defined procedures

You must call explicitly

You can define parameters

Page 5: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 5

Event Procedures

Private Sub ExitButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) _

Handles ExitButton.Click

Me.Close()

End Sub

Parameters

Page 6: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 6

Including Parameters in a User-Defined Sub Procedure

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

Accessibility Sub ProcedureName([ParameterList])

‘ Statements in the procedure

End Sub

Private Sub CalculatePay(ByVal sngHours As Single, _

ByVal sngRate As Single, ByRef sngPay As Single)

‘ Calculate the pay from hours and rate of pay

sngPay = sngRate * sngHours

End Sub

Call CalculatePay(35.9, 95, sngPay)

Page 7: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 7

Passing Variables

Pass by value – make a copy of the data Use ByVal before the parameter name This is the default if you do not specify

Pass by reference – pass the address of the data Use ByRef before the parameter name

Private Sub CalculatePay(ByVal sngHours As Single, _

ByVal sngRate As Single, ByRef sngPay As Single)

‘ Calculate the pay from hours and rate of pay

sngPay = sngRate * sngHours

End Sub

sngHours = 35.9: sngRate = 95.0

Call CalculatePay(sngHours, sngRate, sngPay)

Page 8: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 8

Function Procedures

A Function procedure, typically referred to as a function, is a block of code that performs a specific task

You can also create your own functions, referred to as user-defined functions, in Visual Basic .NET

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 9: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 9

Function Procedures

Accessibility Function FunctionName([ParameterList]) As DataType

‘ Statements in the procedure

Return SomeData

End Function

Private Function CalculatePay(ByVal sngHours As Single, _

ByVal sngRate As Single) As Single

‘ Calculate the pay from hours and rate of pay

Return sngRate * sngHours

End Function

sngHours = 35.9: sngRate = 95.0

sngPay= CalculatePay(sngHours,sngRate)

Page 10: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 10

Using a List Box ControlLesson B Objectives

After completing this lesson, you will be able to:

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

Determine the selected item in a list box

Code a list box’s SelectedValueChanged event

Page 11: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 11

Adding a List Box to a Form

You can 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 12: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 12

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 appears as the second item in the list box, and so on

The first item in the Items has an index of zero

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

Page 13: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 13

Adding Items to a List Box

You 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 14: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 14

The SelectedItem and SelectedIndex Properties

A list box’s SelectItem property and its SelectedIndex property can be used both to determine the item selected in the list box and to select a list box item from code

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 15: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 15

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—either single, (including head of household) or married—and his or her weekly taxable wages

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

Gross pay amount

Marital status

Number of withholding allowances

Page 16: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 16

Completing the CalculateButtonClick Event Procedure

Now that you have created the GetFwtTax

function, you can call the function from

the CalculateButton Click event procedure

Page 17: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 17

Clearing the Contents of the Label Controls

The label controls also 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 18: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 18

Completing the Payroll ApplicationLesson C Objectives

After completing this lesson, you will be able to:

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 19: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 19

Adding an Existing Form to a Solution

The Copyright screen is to be the splash

screen for each custom application

created by the company

Page 20: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 20

Coding the Sub Main Procedure

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

In other words, you can tell the computer to process the Sub Main procedure automatically when an application is started

You enter the Sub Main procedure in a module, which is a file that contains code that is not associated with any specific object in the interface

Page 21: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 21

Creating an Instance of a Form

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

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

Similarly, if the PayrollForm is specified as the Startup object, Visual Basic .NET automatically processes the code contained in the PayrollForm class definition

When the Sub Main procedure is the Startup object, as it is in this case, neither the CopyrightForm class definition nor the PayrollForm class definition will be processed automatically

Page 22: Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures

Tutorial 7: Sub and Function Procedures 22

Using a Form Object’s Show Dialog Method

The form object’s Show dialog method allows you to display a form object on the screen

The syntax of the ShowDialog method is form.ShowDialog(), where form is the name of the object variable that contains the form object’s address