如何声明包含使用记录作为参数的事件的记录

发布于 2025-01-04 03:56:59 字数 318 浏览 1 评论 0 原文

我试图弄清楚如何声明一条记录和一些相互使用的对象事件。问题是无论我以哪种方式声明它们,我都有一个“未声明的标识符”。

那么通过下面的代码,我可以让它们互相使用吗?事件将在对象中使用,记录将被传递并使用到对象的构造函数中。

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

  TMyRecord = record
    OnMyEvent: TMyEvent;
  end;

这可能吗?它需要在 Delphi 7 及更高版本的所有版本中工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠意 2025-01-11 03:56:59

如果您使用的是较新的 Delphi 版本,则可以在记录中声明类型。您可以通过以下方式引用活动记录:

type
  TMyRecord = record
  public type
    TMyEvent = procedure (Sender: TObject; var Rec: TMyRecord) of object;
  public
    OnMyEvent: TMyEvent;
  end;

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;
伪心 2025-01-11 03:56:59

不幸的是,前向声明只允许用于类,而不允许用于记录,所以我知道的唯一方法是使用指针:

PMyRecord = ^TMyRecord;

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

TMyRecord = record
  OnMyEvent: TMyEvent;
end;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文