Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures."— Presentation transcript:

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

2 Tutorial 7: Sub and Function Procedures2 Creating Sub and Function Procedures Lesson 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

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

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

5 Tutorial 7: Sub and Function Procedures5 Event Procedures Private Sub ExitButton_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles ExitButton.Click Me.Close() End Sub Parameters

6 Tutorial 7: Sub and Function Procedures6 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)

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

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

9 Tutorial 7: Sub and Function Procedures9 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)

10 Tutorial 7: Sub and Function Procedures10 Using a List Box Control Lesson 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

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

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

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

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

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

16 Tutorial 7: Sub and Function Procedures16 Completing the CalculateButton Click Event Procedure  Now that you have created the GetFwtTax function, you can call the function from the CalculateButton Click event procedure

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

18 Tutorial 7: Sub and Function Procedures18 Completing the Payroll Application Lesson 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

19 Tutorial 7: Sub and Function Procedures19 Adding an Existing Form to a Solution  The Copyright screen is to be the splash screen for each custom application created by the company

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

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

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


Download ppt "Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures."

Similar presentations


Ads by Google