forcing_20a_20variable_20to_20exist
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| forcing_20a_20variable_20to_20exist [2018/03/31 13:19] – external edit 127.0.0.1 | forcing_20a_20variable_20to_20exist [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| //by Richard Russell, May 2006//\\ \\ Programs frequently need to contain a //cleanup// routine which gets called before the program quits. Such a routine may close files, remove open dialogue boxes, free Windows resources that have been allocated (e.g. memory or graphics objects) etc. So the routine will typically include code similar to the following: | //by Richard Russell, May 2006//\\ \\ Programs frequently need to contain a //cleanup// routine which gets called before the program quits. Such a routine may close files, remove open dialogue boxes, free Windows resources that have been allocated (e.g. memory or graphics objects) etc. So the routine will typically include code similar to the following: | ||
| + | <code bb4w> | ||
| DEF PROCcleanup | DEF PROCcleanup | ||
| CLOSE #Outfile% | CLOSE #Outfile% | ||
| Line 7: | Line 8: | ||
| SYS " | SYS " | ||
| ENDPROC | ENDPROC | ||
| + | </ | ||
| The cleanup code will need to be executed not only on the normal exit from the program but also if a premature exit takes place because of an error (including Escape) or the user clicking on the **Close** button. Therefore it is likely that the routine will be called from both **ON ERROR ** and **ON CLOSE** statements: | The cleanup code will need to be executed not only on the normal exit from the program but also if a premature exit takes place because of an error (including Escape) or the user clicking on the **Close** button. Therefore it is likely that the routine will be called from both **ON ERROR ** and **ON CLOSE** statements: | ||
| + | <code bb4w> | ||
| ON CLOSE PROCcleanup : QUIT | ON CLOSE PROCcleanup : QUIT | ||
| ON ERROR SYS " | ON ERROR SYS " | ||
| + | </ | ||
| But this immediately causes a problem. What happens if the premature exit occurs //before// one or more of the variables used in **PROCcleanup** have been created? The result would be a **No such variable** error, which would activate the ON ERROR routine, which would call PROCcleanup, | But this immediately causes a problem. What happens if the premature exit occurs //before// one or more of the variables used in **PROCcleanup** have been created? The result would be a **No such variable** error, which would activate the ON ERROR routine, which would call PROCcleanup, | ||
| + | <code bb4w> | ||
| variable = variable | variable = variable | ||
| variable += 0 | variable += 0 | ||
| + | </ | ||
| Both these have the same effect: if **variable** already exists it is left unaltered, if **variable** doesn' | Both these have the same effect: if **variable** already exists it is left unaltered, if **variable** doesn' | ||
| + | <code bb4w> | ||
| DEF PROCcleanup | DEF PROCcleanup | ||
| Outfile% += 0 : IF Outfile%<> | Outfile% += 0 : IF Outfile%<> | ||
| Line 19: | Line 26: | ||
| Memory% += 0 : IF Memory%<> | Memory% += 0 : IF Memory%<> | ||
| ENDPROC | ENDPROC | ||
| + | </ | ||
| Of course this relies on the normal values of the variables never being zero, but in general that is the case.\\ \\ You can use this technique whenever you can't be sure whether a variable has been created or not.\\ \\ //Added by JGH, May 2006//\\ \\ This can be improved a little. What would happen if **PROCcleanup** was called again? If **Outfile%** was still nonzero, it would attempt to close a channel that has already been closed. After the cleanup operation the variable should be set to zero, eg **CLOSE# | Of course this relies on the normal values of the variables never being zero, but in general that is the case.\\ \\ You can use this technique whenever you can't be sure whether a variable has been created or not.\\ \\ //Added by JGH, May 2006//\\ \\ This can be improved a little. What would happen if **PROCcleanup** was called again? If **Outfile%** was still nonzero, it would attempt to close a channel that has already been closed. After the cleanup operation the variable should be set to zero, eg **CLOSE# | ||
| + | <code bb4w> | ||
| DEF PROCcleanup | DEF PROCcleanup | ||
| LOCAL temp% | LOCAL temp% | ||
| Line 26: | Line 35: | ||
| Memory% += 0 :IF Memory% | Memory% += 0 :IF Memory% | ||
| ENDPROC | ENDPROC | ||
| + | </ | ||
| This is the technique I use in my BASIC program libraries, such as the Close library. | This is the technique I use in my BASIC program libraries, such as the Close library. | ||
forcing_20a_20variable_20to_20exist.1522502362.txt.gz · Last modified: 2024/01/05 00:17 (external edit)