Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic.NET, Second Edition."— Presentation transcript:

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

2 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

3 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

4 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

5 Programming with Microsoft Visual Basic.NET, Second Edition 5 Sub Procedures (continued) User-defined procedures –You must call explicitly –You can define parameters

6 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

7 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

8 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

9 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

10 Programming with Microsoft Visual Basic.NET, Second Edition 10 Passing Variables (continued) Figure 7-3: Examples of passing variables by value

11 Programming with Microsoft Visual Basic.NET, Second Edition 11 Passing Variables (continued) Figure 7-3: Examples of passing variables by value (continued)

12 Programming with Microsoft Visual Basic.NET, Second Edition 12 Passing Variables (continued) Figure 7-4: Examples of passing variables by reference

13 Programming with Microsoft Visual Basic.NET, Second Edition 13 Passing Variables (continued) Figure 7-4: Examples of passing variables by reference (continued)

14 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

15 Programming with Microsoft Visual Basic.NET, Second Edition 15 Function Procedures (continued) Figure 7-6: User-defined function syntax and example

16 Programming with Microsoft Visual Basic.NET, Second Edition 16 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

17 Programming with Microsoft Visual Basic.NET, Second Edition 17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 Programming with Microsoft Visual Basic.NET, Second Edition 28 Coding the GetFwtTax Function (continued) Figure 7-23: Pseudocode for the GetFwtTax function

29 Programming with Microsoft Visual Basic.NET, Second Edition 29 Coding the GetFwtTax Function (continued) Figure 7-23: Pseudocode for the GetFwtTax function (continued)

30 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

31 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

32 Programming with Microsoft Visual Basic.NET, Second Edition 32 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

33 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

34 Programming with Microsoft Visual Basic.NET, Second Edition 34 Adding an Existing Form to a Solution (continued) Figure 7-30: Copyright form

35 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

36 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

37 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

38 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

39 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

40 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()

41 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

42 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

43 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()


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

Similar presentations


Ads by Google