Programming for Computing 07 - 08

Chapter: Scopes: local and global

Module Admin   Contents, Index   Schedule  
1: Intro to VB    2: First Program and Projects    3: Variables, assignment, Strings    4: Type Conversion, InputBox, Constants    5:Built-in Functions    6: If - decisions    7: Loops - while, for    Example - find smallest    8: Scopes: local, global    9: Writing Procedures.Parameters, Functions   
10: Objects    11: Design    12: Testing    13: Graphics    14: Controls and events    15: Listboxes    16: Arrays    17: Files    18: The Command Line   

Appendices(links etc)   Additional Lectures    Tutorials (not in printed notes)     Selected solutions (not in printed notes)     Assignments    Additions and Errata   

new

The  schedule page has info on what we will do each week.

Why do we do this...

Scopes are to do with splitting large programs into sections (called procedures, or functions). We imagine a 'need to know' structure. For example, the boss of a company does not need to know about some crude data scribbled on a junior's desk. That would be too much information. Such data is termed 'local' to the junior.

 


Section 8.0

Introduction

The scope of an item is the area of program in which it can be used. It is a 'need to know' approach. Some items can sensibly be concealed within procedures (local) rather than cluttering the whole code with global items.

Problem - we want some software for a car park. There are two buttons, marked 'car has entered', and 'car has left'. When either button is clicked, the current number of cars in the park is amended and displayed.

When we drop two buttons on the form, VB creates a 'procedure' - a section of named code - for each button-click event, as in

Private Sub Button1_Click(etc...)
.... done when Button1 clicked
End Sub

Private Sub Button2_Click(etc...)
...done when Button2 is clicked
End Sub
Clearly, we need another variable - e.g. carCount, which is initially 0. One button adds 1, the other subtracts 1. The problem is - how do we declare it?

We can try:

Private Sub Button1_Click(etc...)
dim carCount as Integer = 0
carCount=carCount+1
   ...etc
End Sub

Private Sub Button2_Click(etc...)
dim carCount as Integer = 0
carCount=carCount+1
   ...etc
End Sub

but such variables are termed LOCAL. They can only be used within the procedure they are declared in.
Private Sub Button1_Click()   ' start of procedure
    |
    |   Local variables declared and used here - their extent - 'scope' - is local
    |
End Sub                        ' end of procedure
So, when button1 is clicked, the first instruction that executes is:
   dim carCount as Integer = 0
- this creates the variable. When the end sub is reached, any local variables are wiped out - they are temporary, rather like scrap paper we use and throw away.

So - carCount does not hold its value between clicks - no good for this problem!

In fact, there are 2 variables here, not one:

Analogy: a person is a procedure. Each person has some scrap paper. We ask each one to think of a number, and write it down. They may choose the same one, but probably will not. There is no connection between the numbers. Each person is free to choose.

 


Section 8.1

Advantages of local variables

They are contained within a procedure - they cannot affect other procedures if their values go wrong due to bugs.

The coder of the procedure has free choice of names. If - by coincidence - they choose the same name in use in another procedure, there is no problem.

They are sufficient for many problems. (But not for our seemingly simple carpark!)  


Section 8.2

Disadvantages

They cannot be shared between procedures.  


Section 8.3

Global variables

To tackle the car park, we need a global variable:
Public Class Form1
    dim carCount as integer = 0

    Private Sub Button1_Click()
        carCount=carCount+1
        MessageBox.Show(CStr(carCount))
    End Sub
    '----------------------------------------------------------
    Private Sub Button2_Click()
        carCount=carCount-1
        MessageBox.Show(CStr(carCount))
    End Sub
End Class
What's new:
  • The variable is declared above the two procedures, not inside them. It is inside the class/end class lines however.
  • It is termed 'global' - it can be used by ANY procedure in the program.
  • It can be used to communicate between the procedures.
  • Incidentally, in VB we should call the variable an instance variable of the Form1 class, but GLOBAL is a term that everyone understands, and will be sufficient for your early programming.
  • We will not always show the Class and End Class lines, or the full indentation, because VB does this automatically. They are always there.
BUT - using lots of global variables can produce a program that is hard to debug. If lots of procedures manipulate a variable, who is responsible when it goes wrong?

Analogy - 10 people using the same whiteboard, rather than making their own local notes.

We often need a small number of globals. We can also use parameters (or arguments) to reduce globals - see later.

Note that controls we place on a form - such as a label - are global to the form. Any of our procedures can access them.

If a variable is only used inside one procedure, it should clearly be local.

 


Section 8.4

Name clash?


dim n as integer   ' a global one

Private Sub Button1_Click()
dim n as integer      ' local one
n=n+1      ' which n  - local or global ????

   ...etc
End Sub
 
The global n can be used in all procedures, and there is also a local n. The rule in VB - as in most languages - is that the local one takes precedence if there is a choice. This is normally what we want.  


Section 8.5

Problem

1. Create the car park program, and test it. Make it start with 3 reserved spaces. Then add code (some IF's) to prevent carCount going above 10 (the max capacity).

Key points

Controls are global, so any control can be used in any event.

If you want a variable to be used by more than one control, make that variable global.

Module Admin   Contents, Index