CIS16 Application Development and Programming using Visual Basic.net Chapter Six Sub and Function Procedures
Procedures There are two types of procedures in Visual Basic Sub Procedures perform actions but do not return a value to the calling code Function Procedures Perform actions and return a value to the calling code
Sub Procedures There are two types of Sub procedures Event Sub Procedures Procedures related to events, such as a button’s Click event or a text box’s TextChanged event Private Sub clearButton_Click(sender As Object, e as EventArgs) Handles clearButton.Click avgLabel.text = String.Empty gradeLabel.Text = String.Empty End Sub Independent Sub procedures Processed only when called (invoked) from code Invoked by using the Call statement
Independent Sub Procedures
Including Parameters (variables) in an Independent Sub Procedure A parameter is a memory location that stores an item of data passed to the procedure when the procedure is invoked Each parameter in the parameterList has procedure scope, which means it can be used only within the procedure The data is passed to the procedure through the Call statement’s argumentList, which is a comma-separated list of arguments you want passed to the procedure Call AddNumbers(intNum1, intNum2) Private Sub AddNumbers(x,y) ….. End Sub
Passing Variables passed as arguments, received as parameters Variables have both a value and a unique address for its location in the computer’s internal memory You pass either a copy of the variable’s value or its address to the receiving procedure Passing a copy of the variable’s value is referred to as passing by value Passing its address is referred to as passing by reference
Passing Variables by Value To pass a variable by value, you include the keyword ByVal The variable’s value is passed to the receiving procedure Original value cannot be changed in the procedures
Passing Variables by Reference To pass a variable by reference, you include the keyword ByRef The memory location of the variable is passed to the receiving procedure With ByRef, the original value can be changed in the procedures
Function Procedures A Function procedure returns a value after performing its assigned task Function procedures are referred to as functions The value is returned by the Return statement, which typically is the last statement within a function
Function Procedures (cont'd.)
Function Procedures (cont'd.)
Function Procedures example
Event Signatures Events have a signature comprised of the object raising the event (the sender) and the event being raised (e) Private Sub clearButton_Click(sender As Object, e as EventArgs) Handles clearButton.Click avgLabel.text = String.Empty gradeLabel.Text = String.Empty End Sub
Events with identical actions Can create a procedure that can be executed by more than one event handler - this avoids duplicating code The approach shown below, handles each unique event and each handler executes code in another procedure One procedure for all handlers Individual handlers
Events with identical actions (cont.) Can associate a procedure with more than one object and event - this avoids duplicating code
Desk Checking code Desk-checking is the process of reviewing the program instructions while seated at your desk rather than in front of the computer Also called hand-tracing because you use a pencil and paper to follow each of the instructions by hand
Math Class The math functions in VB.NET are defined in the System.Math class of the .NET Framework To use these functions without qualification, the System.Math namespace must be imported into the project by adding the namespace at the top of the source code: Imports System.Math ... Dim MyVar1 As Double = 2.8 Dim MyVar2 As Double MyVar2 = Round(MyVar1) ' Returns 3 To use these functions with qualification, do not use the imports statement, but instead coding dot notation, code the class name followed by the method name MyVar2 = Math.Round(MyVar1) ' Returns 3
Math Class
“Collection” Controls Collections – an ordered set of items that can be referred to as a unit Common methods are Add, Remove, Clear Common controls are Listbox & ComboBox https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.collection(v=vs.110).aspx
Including a Combo Box in an Interface Displays only one item initially and the user needs to click on the handle of the combo box to view the items in a drop-down Allows the user to select from a number of choices Allows the user to type an entry not on the list Can save space on a form
Including a Combo Box in an Interface (cont.) DropDownStyle property has three values: Simple DropDown (default) DropDownList
Populating a Combo Box – design time Building at design time add items to the list at design time, by typing values into the String Collections Editor You will have to type an item under the text property in order to display the default item at runtime.
Populating a Combo Box - runtime) Build at run time in form load event
Populating a Combo Box - user input You can also allow the user to add their own items using the InputBox function, as follows: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myitem myitem = InputBox(“Enter your Item”) ComboBox1.Items.Add(myitem) End Sub
Removing items from a Combo Box at design time, simply open the String Collection Editor and delete the items at line by line or all at once using the Delete key. Remove method Private Sub Delete_Click(sender As Object, e As EventArgs) Handles Button2.Click ComboBox1.Items.Remove(“Visual Basic 6”) End Sub Clear method Private Sub Btn_Clr_Click(sender As Object, e As EventArgs) Handles Button2.Click ComboBox1.Items.Clear()
Programming a Combo Box (cont.)
Chapter application