| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
3-5: Variable Declarations (continued)
When to use global
variables.
If the value represented by a variable is only needed in one event or function it should not be declared as a global variable. It should be declared locally within the event that requires it. Although Visual Basic (and many other programming languages) allows it, the unnecessary declaration of a variable globally when it should be declared locally is in fact very bad programming style.
Large programs are very complex, consisting of many components. In general the components should be kept as separate from each other as possible. Often a global variable is intended to be shared between the components of the program. It forms a connection between the components. Unnecessary global variables add to the complexity of the software and multiply the possibility of errors.
For example, because a global variable is accessible in any subprogram you can assign it a new value via a statement in any such subprogram. If you have declared a variable globally when it should in fact be local, you might easily (in a large program) inadvertently assign a new value to that variable in some other part of the program.