Search code examples
delphitypesdeclarationcross-reference

How to declare record containing events which use record as parameter


I'm trying to figure out how to declare both a record and a number of object events which use each other. The problem is no matter which way I declare them, I have an "undeclared identifier".

So with the code below, can I get them to use each other? The events will be used in an object, and the record will be passed and used into the object's constructor.

  TMyEvent = procedure(Sender: TObject; var Rec: TMyRecord) of object;

  TMyRecord = record
    OnMyEvent: TMyEvent;
  end;

Is this possible? It needs to work in all versions of Delphi 7 and up.


Solution

  • Unfortunately forward declarations are only allowed for classes but not records, so the only way I know of is to use pointers:

    PMyRecord = ^TMyRecord;
    
    TMyEvent = procedure(Sender: TObject; Rec: PMyRecord) of object;
    
    TMyRecord = record
      OnMyEvent: TMyEvent;
    end;