Lecture (10)Global and Local Variables - Lifetime and Scope

24
1 UNIVERSITI KUALA LUMPUR Malaysia France Institute FSB23103 FSB23103 Object Oriented Programming Lecture 11 Global and local variables - lifetime and scope Mdm Ratnawati Ibrahim

Transcript of Lecture (10)Global and Local Variables - Lifetime and Scope

Page 1: Lecture (10)Global and Local Variables - Lifetime and Scope

1

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

FSB23103 Object Oriented Programming

Lecture 11Global and local variables - lifetime and scope

Mdm Ratnawati Ibrahim

Page 2: Lecture (10)Global and Local Variables - Lifetime and Scope

2

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Topics

• Identifier definition attributes

• Variable lifetime• Variable scope

Page 3: Lecture (10)Global and Local Variables - Lifetime and Scope

3

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Identifier (1)

• An identifier is a name for a class instance variable

• e.g. xpos, ypos

local variable• e.g. box

parameter in the header of a method• e.g. newX, newY

Private xpos As IntegerPrivate ypos As Integer

Dim box As Square

Private sub ChangePositionTo(ByVal newX As Integer _ ByVal newY As Integer )

Page 4: Lecture (10)Global and Local Variables - Lifetime and Scope

4

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Identifier (2)

• Attributes or characteristics of an identifier include name type value (optional) lifetime scope

Dim box As Square

Name Type

Dim total As Integer = 0

ValueName Type

Page 5: Lecture (10)Global and Local Variables - Lifetime and Scope

5

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Variable lifetime (1)

• The lifetime of a variable is the period of time during which the variable exists in computer memory, i.e. it is available for use in a program

• By default, variables exist so long as their containing method/class is loaded in the computer memory

• The value of a variable can change over its lifetime

Page 6: Lecture (10)Global and Local Variables - Lifetime and Scope

6

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Variable lifetime (2)

• e.g. two methods, Test and Bigger

Sub Test() Dim x As Integer = 4 Dim y, diff As integer

y = Bigger(x) diff = y - xEnd Sub

Function Bigger(ByVal a As Integer) As Integer Dim k As Integer = 5 Return a + k End Function

Method Call

MethodBigger

Argument

Method Test

Parameter Local

Variable

Local Variables

Variables declared in the Test method

Bigger method

Variables a and k are created when the function Bigger is called; they cease to exist when the method is exited

4

9

x

y

5diff

4

5

a

k

Page 7: Lecture (10)Global and Local Variables - Lifetime and Scope

7

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Variable lifetime (3)

• Variables declared in a method parameters - in the header

• e.g. a local variables – in the body

• e.g. k• They

are created when the method is called cease to exist when the method is exited

Page 8: Lecture (10)Global and Local Variables - Lifetime and Scope

8

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Variable lifetime (4)

• Variables declared inside a class, but outside any class method definition, exist so long as their containing class is loaded in the computer memory

• e.g. variables paper and ball in the example program

Public Class Form1

Dim paper As Graphics Dim ball As Circle

Private Sub cmdDraw_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdDraw.Click

Page 9: Lecture (10)Global and Local Variables - Lifetime and Scope

9

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Variable scope

• The scope of a variable refers to the region in which it can be referenced in a program

• It is also known as accessibility or visibility• Every variable has a scope which is specified

by the compiler when the variable is declared• Scope types

local global

Page 10: Lecture (10)Global and Local Variables - Lifetime and Scope

10

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Local varaibles (1)

• Variables declared in a method can be referenced only within the method in which they are declared - local scope

• e.g.

Sub Test() Dim x As Integer = 4 Dim y, diff As integer y = Bigger(x) diff = y - xEnd Sub

Function Bigger(ByVal a As Integer) As Integer Dim k As Integer = 5 Return a + k End Function

Variables x, y, and diff can be referenced only within the Test method

Variables a and k can be referenced only within the Bigger method

Page 11: Lecture (10)Global and Local Variables - Lifetime and Scope

11

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Local varaibles (2)

• Variables declared in different methods can have the same name as they are in different scopes

• Variables of different scopes are stored in different areas in computer memory

Page 12: Lecture (10)Global and Local Variables - Lifetime and Scope

12

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Local varaibles (3)

• e.g. Two methods, Test1 and Bigger

Sub Test1() Dim k As Integer = 4 Dim a, diff As integer

a = Bigger(k) diff = a - kEnd Sub

Function Bigger(ByVal a As Integer) As Integer Dim k As Integer = 5 Return a + k End Function

Method Call

MethodBigger

Argument

Method Test

Parameter

Local Variable

Local Variables

Variables declared in the Test method

Bigger method

4

9

k

a

5diff

4

5

a

k

Page 13: Lecture (10)Global and Local Variables - Lifetime and Scope

13

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Global variables (1)

• Variables declared in a class, following the class header and before any method headers, can be referenced by all the methods within the class - global scope within the class

Page 14: Lecture (10)Global and Local Variables - Lifetime and Scope

14

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Global variables (2)

• e.g. The Account class:

Public Class Account

Private balance As Double

Public Sub New(ByVal iniAmount As Double) balance = iniAmount End Sub Public Sub Deposit(ByVal amount As Double) balance += amount End Sub Public Sub Withdraw(ByVal amount As Double) balance -= amount End Sub Public ReadOnly Property currentBalance() As Double Get Return balance End Get End PropertyEnd Class

Global variable

Local variable

Local variable

Page 15: Lecture (10)Global and Local Variables - Lifetime and Scope

15

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Global variables (3)

• e.g. variables paper and ball can be referenced in all the methods within the Form1 class

Public Class Form1

Dim paper As Graphics Dim ball As Circle

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load

paper = picBox1.CreateGraphics ball = New Circle(150, 105, 25, Color.Blue)End Sub

Private Sub cmdDraw_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdDraw.Click ball.Show(paper) End Sub

Global variables

Page 16: Lecture (10)Global and Local Variables - Lifetime and Scope

16

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example (1)

• e.g. Draw a blue box and move it up or down on a PictureBox

PictureBoxpicBox

Size: 250, 200

ButtoncmdDraw

ButtoncmdClose

ButtoncmdDown

ButtoncmdUp

Initially these two buttons are disabled

Page 17: Lecture (10)Global and Local Variables - Lifetime and Scope

17

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example (2)

• Class Square

Public Class Square ' (xpos, ypos) is the coordinate of the top-left corner of ' a Square object on a PictureBox ' side - side length of a square ' tint - colour of a square Private xpos As Integer Private ypos As Integer Private side As Integer Private tint As Color

Public Sub New(ByVal x As Integer, ByVal y As Integer, _ ByVal d As Integer, ByVal c As Color) xpos = x ypos = y side = d tint = c End Sub

Public Sub ChangeYBy(ByVal dy As Integer) ypos += dy End Sub

Global variable

Local variable

Page 18: Lecture (10)Global and Local Variables - Lifetime and Scope

18

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example (3)

• Class Square

Public Sub Show(ByVal paper As Graphics) PaintASquare(paper, tint) End Sub

Public Sub Clear(ByVal paper As Graphics, ByVal c As Color) PaintASquare(paper, c) End Sub

Private Sub PaintASquare(ByVal paper As Graphics, ByVal c As Color) Dim myBrush As SolidBrush = New SolidBrush(c)

paper.FillRectangle(myBrush, xpos, ypos, side, side) End Sub

End Class

Page 19: Lecture (10)Global and Local Variables - Lifetime and Scope

19

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example (4)

• Program code

Public Class Form1 ' Distance of the box moved each ' time Up/Down button is clicked Const dy As Integer = 5;

Dim paper As Graphics Dim box As Square

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Instantiation of object variables paper = picBox.CreateGraphics box = New Square(125, 100, 30, Color.Blue) End Sub

Form MyBase

The constant variable dy, variables paper and box are global variables; they can be referenced in all the methods within the class Form1

Global variables

Page 20: Lecture (10)Global and Local Variables - Lifetime and Scope

20

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example (5)

Event handling methods

Private Sub cmdDraw_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdDraw.Click ' Display the box on paper box.Show(paper)

' Enable Up and Down buttons cmdUp.Enabled = True cmdDown.Enabled = True End Sub

Private Sub cmdClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClose.Click Close() End Sub

Button cmdDraw

Button cmdClose

Page 21: Lecture (10)Global and Local Variables - Lifetime and Scope

21

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example (6)

Private Sub cmdUp_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdUp.Click ' Check if the new position is within the PictureBox If ( box.Y – dy >= 0 ) Then box.Clear(paper, Color.White) box.ChangeYBy(-dy) box.Show(paper) End If End Sub

Button cmdUp

Event handling methods

(0, 0)

(box.X, box.Y)

Page 22: Lecture (10)Global and Local Variables - Lifetime and Scope

22

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example (7)

Private Sub cmdDown_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdDown.Click Dim maxY = picBox.Size.Height – box.Size

' Check if the new position is within the PictureBox if ( box.Y + dy <= maxY ) Then ball.Clear(paper, Color.White) ball.ChangeYBy(dy) ball.Show(paper) End If End SubEnd Class

Button cmdDown

The variable maxY is a local variable – it can be referenced only within this method

Local variable

Event handling method

(0, 0)

(box.X, box.Y)

(picBox.Size.Width, picBox.Size.Height)

maxY

Page 23: Lecture (10)Global and Local Variables - Lifetime and Scope

23

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Class exercise

• e.g. What are displayed in each message box?

Imports Microsoft.VisualBasic.ControlCharsPublic Class Form1 ' globale variable Dim x As Integer = 2 Dim y As Integer = 3

Private Sub cmdCheck_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdCheck.Click ' local variables Dim x As Integer = 4 Dim y As Integer = 5

MessageBox.Show("x = " & CStr(x) & NewLine & "y = " & CStr(y))

display() End Sub

Private Sub display() MessageBox.Show("x = " & CStr(x) & NewLine & "y = " & CStr(y)) End SubEnd Class

Page 24: Lecture (10)Global and Local Variables - Lifetime and Scope

24

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Summary

• An identifier is a name for a variable e.g. class instance variables, local variables,, parameters

• Attributes of an identifier name type value lifetime

• refers to the period of time during which a variable is available for use in a program

scope• refers to the region in which a variable can be

referenced or accessed in a program• Types of variable scopes

local global