lecture (10)global and local variables - lifetime and scope
TRANSCRIPT
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
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
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 )
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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
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
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