By global, it is meant that a global variable, on its second pass through a macro, will start-off with the value it had at the end of its first pass through the macro, and so on and so forth.
➢Each macro index has its own independent global variables.
➢Global variables are shared between all macro tokens of the same DiaryGridLine and the same macro index.
➢Global variables are not shared between tokens of different macro indices.
Since the usefulness of global variables is to let you reuse the previous value of a variable, without initializing it every time, the compiler does not issue any warnings about uninitialized global variables. It is your responsibility to write code (such as the one in the example above) to test whether a global variable is initialized or not, as discussed in the section below.
Initialization
Since global variables keep their value from one execution of a macro to the next, it is important for your code to check if a given global variable has been used before (in which case you do not modify its value at the start of the macro), or if it is the first time (in which case you must initialize it just like any other variable). To see this you call the IsInitialized function, as shown below.
global
nGlob
var
begin
// check if nGlob was initialized before
if IsInitialized(nGlob)
nGlob = nGlob + 1 // increase it
else
nGlob = 1 // initialize it
endif
// ...
end
Examples
Some examples of the use of global variables.
Boolean global variables are usually used to keep track of a "state" in which the diary being produced is, at the time the macro is executed.
•An example might be a bilingual diary where the order of the languages is switched every week. In that case, bg1 might be true if this is the week where language A is first and false if this is the week where language B is first.
•Another example is information that must appear only once in a diary, but to coincide with some other information. For example suppose you put Muslim holidays in a diary, but are required to display a copyright notice at the bottom of the first page where one of these holidays occurs. In this case, bg1 would be false until the first holiday had occurred and true from then on (indicating that the copyright notice should not be displayed again).
String global variables are often used to "grow" a string, accumulating strings from each execution of the macro, into a total string.
Integer global variables are often used to keep some kind of a "counter" value between executions of a macro (see example in next section).
Topic 110031, last updated on 18-Apr-2020