80 likes | 169 Views
6a – Sub Procedures. CSCI N331 VB .NET Programming. Lingma Acheson. Department of Computer and Information Science, IUPUI. Procedures. procedure – a unit of code that executes when called from another place. Not a new face – ‘Sub procedure definition – a button click event
E N D
6a – Sub Procedures CSCI N331 VB .NET Programming Lingma Acheson Department of Computer and Information Science, IUPUI
Procedures • procedure – a unit of code that executes when called from another place. • Not a new face – ‘Sub procedure definition – a button click event Private SubbtnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click …. End Sub Or: lstOutput.Items.Clear() ‘sub procedure call • procedure definition – Defines what to execute when the procedure is called. • procedure call – Defines when to execute the procedure.
Procedures • A procedure must have a name following by (). E.g. lstOutput.Items.Clear()lstOutput.Items.Add(“******”) CDbl(txtInputOne.Text) • Purpose of using procedures – make your code well- organized and easy to understand.
Procedures Using Procedures: Create variables Button click event: Take two user inputs, and store them to variables Add() Subtract() Multiply() Divide() End of event Define Add(): Perform the addition and display the result Define Subtract(): Perform the subtraction and display the result … Define Divide() Perform the division and display the result(handle divide by 0 situation here) Without Using Procedures: Button click event: Create variables Take two user inputs, and store them to variables Perform the addition and display the result Perform the subtraction and display the result Perform the multiplication and display the result Perform the division and display the result (handle divide by 0 situation here) End of event
Sub Procedures • Sub procedure – execute a block of codes when called • Define a Sub procedure: PrivateSubname() ‘actions here … End Sub • The key word Private means this Sub can only be used by this Form. • The Sub procedure call must match with the Sub procedure definition.
Sub Procedures • E.g. a user defined Sub definition and Sub call – Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click intFirst = CInt(nudFirst.Value) intSecond = CInt(nudSecond.Value) Add() ‘Sub call. The program will look for a Sub definition with the same ‘name to decide what to do End Sub ‘Sub definition Private Sub Add() Dim intResult As Integer = 0 ‘intFirst and intSecond are global variables, so they can used by both Subs. intResult= intFirst + intSecond txtAddResult.Text = CStr(intResult) End Sub …
Local Variables and Global Variables • Local Variables: • Purpose: for a specific procedure to use • Scope: only used inside that procedure • Where: created inside a procedure • Global Variables: • Purpose: a variable shared by different procedures • Scope: the form, also called form variables, can be used anywhere in the same form • Where: created at the beginning of the Class, outside of any procedures
Local Variables and Global Variables Global Variables: ‘Create 2 global variables outside of any procedure, can be used anywhere Create variables 1, 2 Add Button click event procedure: Add() End of event procedure Procedure Add(): ‘1 local variable, only used ‘inside this procedure Create variable 3 ‘use global variable 1 and 2 Take the user input and store in variable 1 and 2 ‘use global variable 1 and 2, and ‘local variable 3 Add variable 1 to variable 2 and store the result to variable 3 End of Add procedure Local Variables: Add Button click event procedure: ‘3 local variables, only used ‘inside this procedure Create variables 1, 2, and 3 Take user input and store in variable 1 and 2 Add variable 1 to variable 2 and store the result to variable 3 End of event procedure