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 Page 269, 301 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 Practice  Pass by Ref Handout  Scope Handout

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

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

11 Tutorial 7: Sub and Function Procedures11 EOC Practice  Page 449-450  No1~9

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

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

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

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

16 Tutorial 7: Sub and Function Procedures16 Remove Items from a List Box  When you remove an item from the list, the indexes change for subsequent items in the list. All information about the removed item is deleted.  You can use RemoveAt method to remove a specific item from the list by specifying the actual item to remove from the list.  To remove all items from the list, use the Clear method.  ZipFile – Ex0716

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

18 Tutorial 7: Sub and Function Procedures18 Select an item SelectedIndex  Supported by the.NET Compact Framework.  Overridden. Gets or sets the zero-based index of the currently selected item in a ListBox. SelectedItem SelectedItem  Supported by the.NET Compact Framework.  Gets or sets the currently selected item in the ListBox.  ZipFile – Ex0717

19 Tutorial 7: Sub and Function Procedures19 Select an item  Public Sub SetSelected( _ ByVal index As Integer, _ ByVal value As Boolean _ )IntegerBoolean  Parameters  index  The zero-based index of the item in a ListBox to select or clear the selection for.ListBox  value  true to select the specified item; otherwise, false.  ZipFile – Ex0717

20 Tutorial 7: Sub and Function Procedures20 SelectionMode  The SelectionMode property enables you to determine how many items in the ListBox a user can select at one time and how the user can make multiple-selections.ListBox  When the SelectionMode property is set to SelectionMode.MultiExtended, pressing SHIFT and clicking the mouse or pressing SHIFT and one of the arrow keys (UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW) extends the selection from the previously selected item to the current item. Pressing CTRL and clicking the mouse selects or deselects an item in the list.  When the property is set to SelectionMode.MultiSimple, a mouse click or pressing the SPACEBAR selects or deselects an item in the list.

21 Tutorial 7: Sub and Function Procedures21 Combo Box  Combo box  ZipFile – Ex0718 Text Property  Supported by the.NET Compact Framework.  Overridden. Gets or sets the text associated with this control. Replace Function  Returns a string in which a specified substring has been replaced with another substring a specified number of times.

22 Tutorial 7: Sub and Function Procedures22 For Each  ZipFile – Ex0719  Apply For Each structure to listbox collections For Each … In Next  SelectedItems: Gets a collection containing the currently selected items in the ListBox.

23 Tutorial 7: Sub and Function Procedures23 ListView  ZipFile – Square  ZipFile – Ex0720  A ListView.ColumnHeaderCollection class stores the column headers that are displayed in the ListView control when the View property is set to View.Details.  The ListView.ColumnHeaderCollection stores ColumnHeader objects that define the text to display for a column as well as how the column header is displayed in the ListView control when displaying columns.

24 Tutorial 7: Sub and Function Procedures24 ListView ListView.ColumnHeaderCollection Constructor [Visual Basic] Initializes a new instance of the ListView.ColumnHeaderCollection class. Public Sub New( _ ByVal owner As ListView _ ) Parameters owner The ListView control that owns this collection. Remarks You cannot create an instance of this class without associating it with a ListView control.

25 Tutorial 7: Sub and Function Procedures25 ListView.ColumnHeaderCollection.Add Method  There are a number of ways to add column headers to the collection. The Add method adds a single column header to the collection. Adds a column header to the collection with specified text, width, and alignment settings. Overloads Public Overridable Function Add(String, Integer, HorizontalAlignment) As ColumnHeader

26 Tutorial 7: Sub and Function Procedures26 ListView.ColumnHeaderCollection.Clear Method  The Clear method allows you to remove all column headers from the collection instead of using the Remove method to remove a single column header at a time.

27 Tutorial 7: Sub and Function Procedures27 ListViewItem.ListViewSubItemCollection  When a ListView displays columns, the items and their subitems are displayed in their own columns. To specify which columns subitem data is displayed under, see the ListViewItem.ListViewSubItemCollection class.

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

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

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

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

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

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

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

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