Just emerged from 6 hours debugging an application. Entire exercise resulted from mishandled global variables. My design was badly flawed. I outline it here in hopes my experience will help others avoid this pitfall.
In my app a series of forms are called, one after the other, in a series of button presses, as the user progresses towards completing a particular task. The first form's button dims a handful of global variables, which get passed to all the forms in the series. The user can quit any form, and 'back up' to its predecessor so that adjustments can be made before moving forward. The last form had two buttons, one to save the results of all these machinations while closing all the forms... the other to simply close the last form and return to the first.
I neglected to re-initialize the globals whenever the user returned to the first form from any of the other forms. Just used the dim statement in each form and went on happy as a clam.
When the button on the first form is pressed the first time after that form is opened it initializes the globals just fine. However, when the user returns to that form I forgot that these values were still 'live' carrying values from aborted calculations on other forms... and failed to re-initialize them. The error message resulting from this fiasco had to do with an 'array index out-of-bounds' and occurred intermittantly. This was devilishly tricky to sort out, and has led me to adopt the following rules for globals (especially global arrays):
1. Initialize globals explicitly. I do this by deleting them and then my beginning script dims them again.
2. Make certain the globals are initialized 'before' new calculations or progressions are commenced.
3. When passing globals between linked forms write them down on a piece of paper and account for them as you move from one form to the other.
4. Don't think in one direction only. If the user can 'back up' to an earlier form remember to account for the globals when your earlier form receives focus again, as well as when your form calls yet another.
5. Remember that once they're initialized they exist until destroyed. Just because a form that's using them has closed does not mean that the globals have been closed also, they have not.
6. Just because the user pushes a button to begin a series of calculations does not mean that globals dim'd there are reinitialized. They are not unless you do it explicitly.
-- tom
In my app a series of forms are called, one after the other, in a series of button presses, as the user progresses towards completing a particular task. The first form's button dims a handful of global variables, which get passed to all the forms in the series. The user can quit any form, and 'back up' to its predecessor so that adjustments can be made before moving forward. The last form had two buttons, one to save the results of all these machinations while closing all the forms... the other to simply close the last form and return to the first.
I neglected to re-initialize the globals whenever the user returned to the first form from any of the other forms. Just used the dim statement in each form and went on happy as a clam.
When the button on the first form is pressed the first time after that form is opened it initializes the globals just fine. However, when the user returns to that form I forgot that these values were still 'live' carrying values from aborted calculations on other forms... and failed to re-initialize them. The error message resulting from this fiasco had to do with an 'array index out-of-bounds' and occurred intermittantly. This was devilishly tricky to sort out, and has led me to adopt the following rules for globals (especially global arrays):
1. Initialize globals explicitly. I do this by deleting them and then my beginning script dims them again.
2. Make certain the globals are initialized 'before' new calculations or progressions are commenced.
3. When passing globals between linked forms write them down on a piece of paper and account for them as you move from one form to the other.
4. Don't think in one direction only. If the user can 'back up' to an earlier form remember to account for the globals when your earlier form receives focus again, as well as when your form calls yet another.
5. Remember that once they're initialized they exist until destroyed. Just because a form that's using them has closed does not mean that the globals have been closed also, they have not.
6. Just because the user pushes a button to begin a series of calculations does not mean that globals dim'd there are reinitialized. They are not unless you do it explicitly.
-- tom
Comment