如何定义一个有返回值的事件,触发并响应它 ( 积分: 100 )

  • 主题发起人 主题发起人 sunjunfeng4
  • 开始时间 开始时间
S

sunjunfeng4

Unregistered / Unconfirmed
GUEST, unregistred user!
一个类要从外部获得一些信息,我想通过事件机制,当需要获得外部的信息时,就触发事件,然后外部通过事件响应把值传给这个类
 
TTestEvent = function (I: Integer): string of Object;

TTest = class
private
FOnTest: TTestEvent;
published
property Test: TTestEvent read FOnTest write FOnTest;
end;
 
上面的代码,如果触发,如果响应,我是菜鸟
 
我这样做了,可是提示错误
type
TGetRegisterModeEvent = Function(sender:TObject):TRegisterMode of Object; //EntityConfig向RegisterModeFrame索要注册类型的数据事件
private
FOnGetRegisterMode:TGetRegisterModeEvent; //触发外部事件
published
property OnGetRegisterMode :TGetRegisterModeEvent read FOnGetRegisterMode write FOnGetRegisterMode;
end;
错误 Field definition not allowed after methods or properties
 
Field definition not allowed after methods or properties 提示:
变量声明不能放在方法或属性后面,楼主好好检查看看
 
property 定义后面不能有function procedure 或FName: Type这些定义

FTest: string;
function Test: string;
procedure Test;
property OnGetRegisterMode: TGetRegisterModeEvent;
这样就不会错了


property OnGetRegisterMode: TGetRegisterModeEvent;
FTest: string;
function Test: string;
procedure Test;
这样就出现了上面的错误提示
 
绝对没有把变量声明放在属性后,只是放在private 域里,但放在public 域就没问题了
全部代码
private
function GetEntityConfig:TEntityConfig;
Procedure SetEntityConfig (aEntityConfig:TEntityConfig);
public
FOnGetRegisterMode:TGetRegisterModeEvent; //触发外部事件--这两个放在private 就有问题
FOnSetRegisterMode:TSetRegisterModeEvent;//触发外部事件
Procedure ClearEntityConfig;
Property EntityConfig:TEntityConfig read GetEntityConfig write SetEntityConfig;
published
property OnGetRegisterMode :TGetRegisterModeEvent read FOnGetRegisterMode write FOnGetRegisterMode;
property OnSetRegisterMode : TSetRegisterModeEvent read FOnSetRegisterMode write FOnSetRegisterMode;
end;
 
TEntityConfig = function : string of Object;
TGetRegisterModeEvent = function : string of Object;
TSetRegisterModeEvent = TGetRegisterModeEvent;

TTest = class
private
FOnGetRegisterMode:TGetRegisterModeEvent; //触发外部事件--这两个放在private 就有问题
FOnSetRegisterMode:TSetRegisterModeEvent;//触发外部事件
function GetEntityConfig:TEntityConfig;
Procedure SetEntityConfig (aEntityConfig:TEntityConfig);
public
Procedure ClearEntityConfig;
Property EntityConfig:TEntityConfig read GetEntityConfig write SetEntityConfig;
published
property OnGetRegisterMode :TGetRegisterModeEvent read FOnGetRegisterMode write FOnGetRegisterMode;
property OnSetRegisterMode : TSetRegisterModeEvent read FOnSetRegisterMode write FOnSetRegisterMode;
end;
能通过没出错 D7+XPsp2
 
ANiDelphi的方法是可行的.
VCL源代码里面也有这样的实现.不过VCL里面更多的是按地址串参数的方式获取事件的返回值
 
接受答案了.
 
后退
顶部