Bug Hunt Starter
• Use your developing
  knowledge of VB to find
  and correct the errors in
  the example program.
Objectives
• Identify the scope of variables and distinguish
  between local and global identifiers

• Identify and explain the use of constants and
  variables in algorithms and programs.
Variables (general)
• A variable is a name used to refer to a particular
  memory location that is used to store data

• Value can change while the program is running.

• Purpose if to allow the programmer to write code
  that manipulates the data without knowing, yet,
  what the data will be.
What data types that can be
  applied to variables?

String             Integer                Byte




Long               Double                Decimal




         Boolean             Date/Time
Types of variables
         • Declared at the beginning of the
           code and is available throughout
Global     the code, including all its
           subroutines.




         • Declared and used inside a
           module or subroutine and is only
Local      available in the module. Created
           when subroutine is called and
           destroyed when subroutine exits.
Program
Global      Form 1

 Local   Sub Procedure

 Local   Sub Procedure

 Local   Sub Procedure

 Local   Sub Procedure
Program
Global (to form)          Form 1   Global (to form)      Form 2

 Local             Sub Procedure    Local         Sub Procedure

 Local             Sub Procedure    Local         Sub Procedure

 Local             Sub Procedure    Local         Sub Procedure

 Local             Sub Procedure    Local         Sub Procedure
Program
Global (to form)   Form 1    Global (to form)   Form 2    Global (all)    Module

  Local      Sub Procedure     Local      Sub Procedure     Local        Function

  Local      Sub Procedure     Local      Sub Procedure     Local        Function

  Local      Sub Procedure     Local      Sub Procedure

  Local      Sub Procedure     Local      Sub Procedure
In action…
Variables top tips!
Use of spaces is not allowed. Use underscore or capitalisation
to make variables easier to spot. E.g. TimeOfArrival or
Time_of_Arrival

Give them sensible names, avoiding reserved words (IF, END,
OR, PRINT)


Use prefixes to help you remember data type e.g.
strFirstname, intAge



Select an appropriate data type – why?
Test yourself
1. What statement do you use to declare a local
   variable in VB?
2. What statement do you use to declare a global
   variable in VB?
3. What operator do you use to assign a value to a
   variable?
4. In terms of security, why is a local variable better
   than a global variable?
5. How should I create a variable that can we used
   program wide and available to all forms?
Constants
• Used where values will not change

• Examples?

Declared as:
        Const Public VAT as Decimal = 17.5

• Doesn’t require program to lookup value, always
  known – time efficient.
In summary..

Global Variables: a variable declared at the
beginning of a program and accessible from
anywhere in the program

Local Variable: a variable declared in a program
block & accessible only within the program block

Constant: a value that does not change throughout
the program.

Variables and constants

  • 1.
    Bug Hunt Starter •Use your developing knowledge of VB to find and correct the errors in the example program.
  • 2.
    Objectives • Identify thescope of variables and distinguish between local and global identifiers • Identify and explain the use of constants and variables in algorithms and programs.
  • 3.
    Variables (general) • Avariable is a name used to refer to a particular memory location that is used to store data • Value can change while the program is running. • Purpose if to allow the programmer to write code that manipulates the data without knowing, yet, what the data will be.
  • 4.
    What data typesthat can be applied to variables? String Integer Byte Long Double Decimal Boolean Date/Time
  • 5.
    Types of variables • Declared at the beginning of the code and is available throughout Global the code, including all its subroutines. • Declared and used inside a module or subroutine and is only Local available in the module. Created when subroutine is called and destroyed when subroutine exits.
  • 6.
    Program Global Form 1 Local Sub Procedure Local Sub Procedure Local Sub Procedure Local Sub Procedure
  • 7.
    Program Global (to form) Form 1 Global (to form) Form 2 Local Sub Procedure Local Sub Procedure Local Sub Procedure Local Sub Procedure Local Sub Procedure Local Sub Procedure Local Sub Procedure Local Sub Procedure
  • 8.
    Program Global (to form) Form 1 Global (to form) Form 2 Global (all) Module Local Sub Procedure Local Sub Procedure Local Function Local Sub Procedure Local Sub Procedure Local Function Local Sub Procedure Local Sub Procedure Local Sub Procedure Local Sub Procedure
  • 9.
  • 10.
    Variables top tips! Useof spaces is not allowed. Use underscore or capitalisation to make variables easier to spot. E.g. TimeOfArrival or Time_of_Arrival Give them sensible names, avoiding reserved words (IF, END, OR, PRINT) Use prefixes to help you remember data type e.g. strFirstname, intAge Select an appropriate data type – why?
  • 11.
    Test yourself 1. Whatstatement do you use to declare a local variable in VB? 2. What statement do you use to declare a global variable in VB? 3. What operator do you use to assign a value to a variable? 4. In terms of security, why is a local variable better than a global variable? 5. How should I create a variable that can we used program wide and available to all forms?
  • 12.
    Constants • Used wherevalues will not change • Examples? Declared as: Const Public VAT as Decimal = 17.5 • Doesn’t require program to lookup value, always known – time efficient.
  • 13.
    In summary.. Global Variables:a variable declared at the beginning of a program and accessible from anywhere in the program Local Variable: a variable declared in a program block & accessible only within the program block Constant: a value that does not change throughout the program.

Editor's Notes

  • #7 Global variables across forms = variable in module