How to declare record containing events which use

2019-04-11 22:34发布

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.

2条回答
对你真心纯属浪费
2楼-- · 2019-04-11 23:11

If you're using a more recent Delphi version, you can declare types within records. Here is how you can reference the record from your event:

type
  TMyRecord = record
  public type
    TMyEvent = procedure (Sender: TObject; var Rec: TMyRecord) of object;
  public
    OnMyEvent: TMyEvent;
  end;
查看更多
不美不萌又怎样
3楼-- · 2019-04-11 23:16

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;
查看更多
登录 后发表回答