Part VII
Using Multiple Copies of a Grid Function

Yikes! We thought we had everything under control. What's going on here? Will our real calendar grid please step forward. The code for this unhappy state of affairs is in calendar6.x.
The use of STATIC variables in grid functions
You may recall (or not) that any variables that are defined
with a scope of STATIC remain accessible within the function. However, they
also are common to each and every instance of that function. So, the same thing
applies to any variables that are defined as STATIC in our new grid function.
Gotcha!
In DsCalendar, we have defined quite a few STATIC variables.
STATIC initialize
STATIC month$[], weekday$[], md[]
STATIC year, month, day, weekday, curDayGrid, lastDayGrid
So, just remove a few of the offending ones! (No, just joking). The problem lies with our variables that change during the course of operations of our grid. Thus, initialize, year, month, day, weekday, curDayGrid, lastDayGrid, which all change at some point in time in our grid function, are the problem. In our example, both copies of DsCalendar ( ) are busy sharing and changing the same STATIC variables. No wonder they are confused. How do we solve this problem?
Creating a Value Array
Again fortunately, we can get out of this situation by creating
a value array for our grid function. Each grid function is allowed to create
one XLONG integer array that can be used to store our variables for each grid.
In DsCalendar ( ), we create a value array by adding the function XuiCreateValueArray(
). It can be placed SUB Create, just beneath XuiCreateGrid(
):
SUB Create
IF (v0 <= 0) THEN v0 = 0
IF (v1 <= 0) THEN v1 = 0
IF (v2 <= 0) THEN v2 = designWidth
IF (v3 <= 0) THEN v3 = designHeight
XuiCreateGrid (@grid, DsCalendar, @v0, @v1, @v2, @v3, r0, r1,
&DsCalendar())
XuiCreateValueArray (grid, #CreateValueArray, $LastValue, 0, 0,
0, 0, 0 )
...
In this case $LastValue represents the upperBounds of our value array.
Create Constants to replace STATIC variables
Now we have to change our variables into constants. If your
variable is named myVariable, then it might be a good idea to name your constant
$MyVariable inorder to keep track of them more easily in the next step. Here
is our new list of constants that will replace our STATIC variable list.
$Initialize = 0
$YearValue = 1
$MonthValue = 2
$Day = 3
$WeekdayValue= 4
$CurDayGrid = 5
$LastDayGrid = 6
$LastValue = 6
How do we use these constants?
In every subroutine where we use our variables, like the
variable lastDayGrid, we must add one of two functions XuiGetValue ( )
or XuiSetValue ( ). The last argument in these functions is the index
number of our value array which is represented by our list of constants.
XuiGetValue (grid, #GetValue, @lastDayGrid, 0, 0, 0, 0, $LastDayGrid)
XuiSetValue (grid, #SetValue, lastDayGrid, 0, 0, 0, 0, $LastDayGrid)
As an example, in the SUB DaySelection, we initially had two lines which used two STATIC variables:
lastDayGrid = curDayGrid
curDayGrid = r0
After our change, we know have this:
XuiGetValue (grid, #GetValue, @curDayGrid, 0, 0, 0, 0, $CurDayGrid)
lastDayGrid = curDayGrid
curDayGrid = r0
XuiSetValue (grid, #SetValue, lastDayGrid, 0, 0, 0, 0, $LastDayGrid)
XuiSetValue (grid, #SetValue, curDayGrid, 0, 0, 0, 0, $CurDayGrid)
Our program calendar7.x contains all of the necessary changes.
Avoid using STATIC variables or arrays in grid functions
Ok, this is not much fun to do retroactively. So as a suggestion, it may be
wise to use XuiCreateValueArray( ) from the very start of programming
our grid function. In this way you can avoid using variables whose scope is
defined as STATIC in your grid function.
Next In Part VIII
(c) 2000 David SZAFRANSKI