Download presentation
Presentation is loading. Please wait.
Published byTauno Jurkka Modified over 6 years ago
1
CIS16 Application Development and Programming using Visual Basic.net
Chapter Six Sub and Function Procedures
2
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
3
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
4
Independent Sub Procedures
5
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
6
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
7
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
8
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
9
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
10
Function Procedures (cont'd.)
11
Function Procedures (cont'd.)
12
Function Procedures example
13
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
14
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
15
Events with identical actions (cont.)
Can associate a procedure with more than one object and event - this avoids duplicating code
16
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
17
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
18
Math Class
19
“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
20
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
21
Including a Combo Box in an Interface (cont.)
DropDownStyle property has three values: Simple DropDown (default) DropDownList
22
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.
23
Populating a Combo Box - runtime)
Build at run time in form load event
24
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
25
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()
26
Programming a Combo Box (cont.)
27
Chapter application
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.