Sometimes it is required to divide values of variables between event procedures. For example, if application fulfills calculations, always using the identical interest rate, its value should be accessible to all procedures of the form. Such variables name variables of level of the form or unit level (form-level, module-level). On fig. 5-4 areas of visibility for variables in the project Visual Basic with one form are shown. As well as operator Option Explicit, the declaration of variables of level of the form happens in section Declarations. For example, if openly window Code, it is possible to select (Declarations) for object (General) and to enter: Dim InterestRate As Currency
-
Variable value InterestRate will be accessible to all procedures connected to the given form.
-
All changes of this variable imported to any of procedures, will be saved.
It is obvious that the last point calls for careful change of values of variables of level of the form. Any information transferred between event procedures, is a fertile field for software failures. Moreover, frequently such errors are heavy for correcting.
Basically, for local variables and variables of level of the form it is possible to use identical names, though it not absolutely good idea. Any operator Dim at procedure level has advantage before similar at form level, therefore such variables will be local. But possibility of usage of the information containing in variables of level of the form is thus lost. That is duplication of names leads to inaccessibility to procedure of variable value of level of the form. Visual Basic, in turn, does not inform on it to the programmer. Here one more reason on which it is recommended to declare explicitly local variables by means of operator Dim.
Council: Some programmers like to use a prefix f (for example, flnterest) for variables of level of the form, and a letter g - at a designation of global variables (for example, ginterest). It allows to recognize area of visibility for a specific variable literally at a single glance.
Reserved word Private. Visual Basic 5 uses this new word Private for a designation of variables of level of the form. Private works similarly to operator Dim, and it can be used instead of this operator in section (General) for the form. The sense consists in separating variables of level of the form from so-called global (or public) variables.
We save values
When Visual Basic causes event processing procedure, old values of local variables are destroyed. They accept values by default. (As it was often marked, it is better to initialize them anew.) Such variables are called as dynamic. However it is not enough of it in some situations. For example, it is necessary to trace, how many time the command button has been pushed. If the counter is dropped, count all time it will be impossible. Basically, it is possible to use variables of level of the form for such purposes, but to apply them only to information interchange is better. The majority of programmers use such approach, only if it is necessary to consider and to other procedures.
Output is usage of static variables. Such variables are not initialized repeatedly at a procedure call. Besides, that they are ideally suited for counters, them use to make control items visible or invisible (generally speaking, for any operations with boolean properties), and also in the debug purposes. To declare in procedure static variable, it is necessary to replace the operator Dim with reserved word Static:
Static Counter As Integer, IsVisibie As Boolean
The example of processing procedure of event with the command button which counts number of clicks is more low resulted and deduces total value:
Private Sub Commandl_Click ()
' the Given procedure uses static variable for count of clicks
Static Counter As Integer ' the Counter begins with zero
Counter = Counter + 1
Print Counter End Sub
At the first click to the counter value by default, that is 0 is appropriated. Then Visual Basic adds to it 1 and then deduces result.
Sometimes it is required, that all variables in procedure were static. For this purpose it is necessary to add reserved word Static before line Private Sub with which any procedure begins:
Static Private Sub Commandl Click ()
The remark: Variables of level of the form and unit level always save the values. The problem consists that any procedure can change them, and it can lead to errors. And when it is necessary to save values of variables in breaks between procedure usage, to apply static variables, than variables of level of the form or the unit is better.
Picture 5-4. Areas of visibility of variables.
Back on the content