1 / 8

6a – Sub Procedures

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

raheem
Télécharger la présentation

6a – Sub Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6a – Sub Procedures CSCI N331 VB .NET Programming Lingma Acheson Department of Computer and Information Science, IUPUI

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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 …

  7. 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

  8. 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

More Related