Procedure pointers (callbacks) in QuickBASIC

Problem

The QuickBASIC language does not have a built-in feature to call a procedure through a pointer (or at least not one intended to be used that way) - that is, a SUB or FUNCTION cannot be given another SUB or FUNCTION to call. If the same piece of code is to be used to perform different tasks, the only way to do so is to use e.g. SELECT CASE at the point of the call.

Solutions

UEVENT

The UEVENT feature, which is an overlooked part of the language (I have never seen it used), can be [ab]used as a callback mechanism:

DECLARE SUB SetUEvent ()  'part of QB.LIB/QB.QLB; run QB with the /L option

'To set the callback:
ON UEVENT GOSUB Callback  'Set the label to be called by SetUEvent
UEVENT ON                 'Enable UEVENT processing

'To call the callback:
SetUEvent
Dummy:  'Ensure UEVENT processing if program is compiled without /V option

END

Callback:
  PRINT "Hello world"
  RETURN

The same technique is demonstrated in Q32164.

Advantages

Disadvantages

My PROCPTR library

In 2013, having known about UEVENT but not knowing how simple it is to use, I wrote a library (named PROCPTR) as a proof of concept. You can download it along with an example program.

It is written in NASM assembly code; see my Using NASM code with QuickBASIC page.

Advantages

Disadvantages

BoostQB

BoostQB (or boostqb for the nihilistic) is a library that includes, among other things, the most polished implementation of procedure pointers. I didn't know about it until I started writing this page.

Advantages

Disadvantages


First published on .
Last updated on .

Table of contents

Contact me