Passing Variables to ON ERROR and ON Event Handlers (21863)
The information in this article applies to:
- Microsoft QuickBASIC 4.0
- Microsoft QuickBASIC 4.0b
- Microsoft QuickBASIC 4.5
- Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
- Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
- Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
- Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1
This article was previously published under Q21863 SUMMARY
This article discusses how a QuickBasic program can pass variables to
ON ERROR and ON "event" handler routines compiled with subprograms
that are separate from the main program.
(The different ON "event" statements are ON TIMER GOSUB, ON KEY GOSUB,
ON PLAY GOSUB, ON STRIG GOSUB, and ON PEN GOSUB.)
MORE INFORMATION
Variables can be passed through the COMMON SHARED or DIM SHARED
statement to ON ERROR and ON "event" handler routines compiled
separately from the main program. Variables that do not follow a
COMMON SHARED or DIM SHARED statement are not accessible to the
handler routines (unless the handler is in the same source file as the
main program).
The following is an example of a separately compiled subprogram that
shares a flag variable with its error handler routine:
' TEST.BAS
COMMON SHARED FLAG 'Used if FLAG is to be global between modules
'DIM SHARED FLAG 'Will also work, if FLAG is to be global only
'to this module
errortrap:
' The error is handled here:
PRINT "Error Number Trapped="ERR
FLAG=5
RESUME NEXT ' Resumes to line after where error occurred.
SUB test STATIC
ON ERROR GOTO errortrap
FLAG=0
ERROR 5 ' Forces a test error number 5 to occur.
PRINT "FLAG=",FLAG ' Execution resumes here after the error.
END SUB
This program is called by the following separate main program module:
' MAIN.BAS
CALL TEST
First, compile as follows:
BC MAIN/X;
BC TEST/X;
You then can LINK MAIN+TEST; run the resulting program as an example.
You cannot use the linenumber or linelabel option of RETURN or RESUME
in conjunction with subprograms.
In QuickBasic versions 1.0, 1.01, 1.02, 2.0, 2.01, 3.0, and 4.0, error
and event handling routines are local to each separately compiled
module. A given error or event handling routine serves all subprograms
compiled in the same module. The ON ERROR GOTO or ON event GOSUB
statement should appear in each subprogram that is compiled separately
from the main program.
QuickBasic versions 4.0b and 4.5, Microsoft Basic Compiler versions
6.0 and 6.0b, and Microsoft Basic PDS versions 7.0 and 7.1 offer
global error handling, which is described in a separate article. For
more information, query on the following words:
global and error and handling and QuickBasic
Modification Type: | Minor | Last Reviewed: | 8/16/2005 |
---|
Keywords: | KB21863 |
---|
|