Download presentation
Presentation is loading. Please wait.
Published byἈριστόδημε Δουμπιώτης Modified over 5 years ago
1
3.2 Working with Data Scope of variables 29/07/2019
2
Learning Objectives Explain the concept of lifetime.
State the lifetime of variables declared with the Dim statement. Explain how to extend this lifetime. Explain what the scope of a variable refers to. Name the two main scope levels of variables, state their ‘scopes’ and explain how to declare variables with each type of scope. Explain the concept of “narrow scope”. Explain how to produce Message Boxes. State the general form of code which increments variables. 29/07/2019
3
Lifetime The lifetime of a variable represents the period of time during which it can hold a value. Its value can change over its lifetime, but it always holds the last defined value. A variable declared with the Dim statement has a lifetime equal to its scope. i.e. The variable is reset every time the Dim statement is encountered. 29/07/2019
4
Scope of a variable Refers to the part of the program code that can use a variable. There are two main levels of scope: Local Global 29/07/2019
5
Scope of a variable Local scope Global scope Level Description
Available to all code within the procedure in which it is declared Global scope Available to all code within the module, class, or structure in which it is declared i.e. defined outside a procedure. 29/07/2019
6
MsgBox You inform a user using labels but an MsgBox is another method of informing the user. It is useful because it less easy to ignore a MsgBox. 29/07/2019
7
Incrementing Variables
… = … + increment value e.g. Number = Number + 1 Add on 1 to the variable Number each time. Same Variable Name / Identifier Note that this line makes no sense mathematically. Imagine: x = x + 1 However, remember that in programming = is an arithmetical operator meaning to assign. It does not mean “it is equal” it means “make it equal”. Left Side -> Right Side You can alternatively use … += increment value 29/07/2019
8
Incrementing Variables
For example: Number = Number + 1 OR Number += 1 Means add on another 1 to the variable Number each time from the previous Number (so if Number starts from 0, it will become 1, then 2, then 3, etc….). Total = Total + NewNumber Total += NewNumber Means add on a new number to the variable Total each time from the previous Total. 29/07/2019
9
Program 3.2 Global Variables
Specification: Demonstrate the difference between local and global variables. 29/07/2019
10
Program 3.2 Global Variables
Create a new project named ‘Global Variables’. Change the form’s Text property to ‘Global Variables’. Place two labels in the upper left and right areas of the form and one button control in the lower centre of the form. 29/07/2019
11
Properties to set: Upper left label: Upper right label:
Text: Number of clicks Size: 14 Bold: True Upper right label: Name: lblClicks BorderStyle: FixedSingle Text: (Blank) Note that the Border allows the control to remain visible. If you make the text property of a label blank without creating a border first you will find the label becomes invisible and almost impossible to move again even though VB still knows it exists. Lower centre button: Name: butOK Text: OK Size: 18 Bold: True
12
Program 3.2 Global Variables
Double click the OK button and enter the following code inside its procedure template: Dim Number As Integer Number = Number + 1 ‘Increments the count of the number of clicks by one. lblClicks.Text = Number ‘Display the number of clicks. Run the program and click the button a few times. The program keeps saying you have only clicked once. Why? 29/07/2019
13
Local Variables Between each click the variable: ‘Number’ ceases to exist (i.e. its lifetime is only as long as the procedure). It is reset every time the procedure is called with the Dim statement. Local variables are said to have a scope limited to within the module/block/subroutine/procedure (button) it is declared in. 29/07/2019
14
Program 3.2 Global Variables
Move up out of the procedure to the beginning, before (or outside of) any procedures or “modules”, and create a blank line. Select and move the Dim statement here. 29/07/2019
15
Program 3.2 Global Variables
Run the program again. It now works. Add the comment to the following line: Dim Number As Integer ‘Declare the variable Number globally, so that it continues to exist while the program is running, this means its value is “remembered” from one button click to another. 29/07/2019
16
Global Variables Global variables are said to have a scope limited only to entire lifetime of the program. Meaning that it is valid and “remembered” from the time the program is run to the time the program is stopped (not just within a procedure (button) in a program – like local variables are).
17
Narrow scope You may be thinking: “To avoid this issue why don’t we declare all variables globally?” However, it is good programming practice and efficient to declare variables with as narrow a scope as possible. Certain errors/problems can occur if you don’t: Name Conflict Avoidance Several different procedures can use the same variable name/identifier. As long as each instance is declared as a (local) procedure variable, each procedure recognizes only its own version of the variable. Debugging Problems: Difficult to debug as difficult to find where variable value was changed as different procedures can modify the value.
18
Extension “Running Total” Program
Write a program to allow a user to enter a series of numbers in a text box and see their running total. One button: Declare a variable to store the number entered and one to hold the total so far. Store number from the text box in a variable. Calculate total so far. Display total. 29/07/2019
19
Plenary Explain the concept of lifetime.
The lifetime of a variable represents the period of time during which it can hold a value. State the lifetime of variables declared with the Dim statement. A variable declared with the Dim statement has a lifetime equal to its scope. Explain how to extend this lifetime. Declare variables globally. 29/07/2019
20
Plenary State the length of this extension.
Variables declared with Static statement hold their values as long as the application it was declared in is running i.e. have lifetimes as long as the lifetime of the application. 29/07/2019
21
Plenary Explain what the scope of a variable refers to.
Refers to the part of the program code that can use a variable. Should we declare all variables globally. 29/07/2019
22
Narrow scope No, we should not declare all variables globally because it is good programming practice and efficient to declare variables with as narrow a scope as possible. Certain errors/problems can occur if you don’t: Name Conflict Avoidance Several different procedures can use the same variable name/identifier. As long as each instance is declared as a (local) procedure variable, each procedure recognizes only its own version of the variable. Memory Consumption (Local) Procedure variables consume memory only while their procedure is running. Their memory is released when the procedure finishes. (Global) Module variables obviously use memory until the module finishes i.e. longer. 29/07/2019
23
Plenary Name the two main scope levels of variables, state their ‘scopes’ and explain how to declare variables with each type of scope. 29/07/2019
24
Scope of a variable Level Description (Local) Procedure scope
Available to all code within the procedure in which it is declared (Global) Module scope Available to all code within the module, class, or structure in which it is declared i.e. defined outside procedure. 29/07/2019
25
Plenary How do we increment a variable 29/07/2019
26
Incrementing Variables
… = … + increment value ‘Add a value to the number Same Variable Name / Identifier Note that this line makes no sense mathematically Imagine: x = x + 1 However, remember that in programming the assignment statement “=“ does not mean “it is equal” it means “make it equal”. 29/07/2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.