6a-SubProcedures - Department of Computer and Information

advertisement

CSCI N331 VB .NET Programming

6a – Sub Procedures

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

2

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 wellorganized and easy to understand.

3

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

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)

Sub Procedures

Sub procedure – execute a block of codes when called

• Define a Sub procedure:

Private Sub name ()

‘actions here

End Sub

• The key word

Private

used by this Form.

means this Sub can only be

• The Sub procedure call must match with the Sub procedure definition.

5

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

6

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

7

Local Variables and Global Variables

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

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

Download