type
TMyClass = class
type
PMyClass = ^TMyClass;
TMyEvent = procedure(Sender: PmyClass) of object;
public
FTest: TMyEvent;
procedure Test;
end;
procedure TMyClass.Test;
begin
FTest(@Self); //error!
end;
我该怎么做?
答案 0 :(得分:5)
你想做什么?当然,由于您还没有为FTest
分配TMyEvent
,因此无法致电PMyClass
! TMyEvent
和TMyClass
真的应该是type
PMyClass = ^TMyClass;
TMyClass = class;
TMyEvent = procedure(Sender: PMyClass) of object;
TMyClass = class
private
FEvent: TMyEvent;
FSomeValue: string;
public
property OnSomething: TMyEvent read FEvent write FEvent;
property SomeValue: string read FSomeValue write FSomeValue;
procedure Test;
end;
TSomeOtherClass = class
public
class procedure MyEvent(Sender: PMyClass);
end;
...
procedure TForm6.FormCreate(Sender: TObject);
var
o: TMyClass;
begin
o := TMyClass.Create;
o.SomeValue := 'This is a test.';
o.OnSomething := TSomeOtherClass.MyEvent;
o.Test;
end;
{ TMyClass }
procedure TMyClass.Test;
begin
FEvent(@Self);
end;
{ TSomeOtherClass }
class procedure TSomeOtherClass.MyEvent(Sender: PMyClass);
begin
ShowMessage(TMyClass(Sender^).SomeValue);
end;
的一部分吗?
至少这完美无缺:
TMyEvent
当然,您也可以在同一个对象中使用type
TMyClass = class
private type
PMyClass = ^TMyClass;
TMyEvent = procedure(Sender: PMyClass) of object;
private
FEvent: TMyEvent;
FSomeValue: string;
public
procedure TheEventHandler(Sender: PMyClass);
property OnSomething: TMyEvent read FEvent write FEvent;
property SomeValue: string read FSomeValue write FSomeValue;
procedure Test;
end;
...
procedure TForm6.FormCreate(Sender: TObject);
var
o: TMyClass;
begin
o := TMyClass.Create;
o.SomeValue := 'This is a test.';
o.OnSomething := o.TheEventHandler;
o.Test;
end;
{ TMyClass }
procedure TMyClass.Test;
begin
FEvent(@Self);
end;
procedure TMyClass.TheEventHandler(Sender: PMyClass);
begin
ShowMessage(TMyClass(Sender^).SomeValue);
end;
:
{{1}}