Delphi – anonymous method as a function result

What I want to do is assign an anonymous method as a function result to a variable of the same type. Delphi complains that an appointment cannot be made. Obviously something about Delphi I want to assign the “GetListener” function Instead of the result of the same function. Any help is greatly appreciated.

type
TPropertyChangedListener = reference to procedure (Sender: TStimulus);
< br /> TMyClass = class
function GetListener:TPropertyChangedListener
end;


....

var MyClass: TMyClass;< br /> Listener: TPropertyChangedListener;
begin
MyClass:= TMyClass.create;
Listener:= MyClass.GetListener; // Delphi compile error: E2010 Incompatible types: TPropertyChangedListener' and'Procedure of object'

end;

Use the following syntax:

< /p>

Listener:= MyClass.GetListener();

I wrote the following example to clarify the difference between MyClass.GetListener() and MyClass.GetListener assignment :

type
TProcRef = reference to procedure(Sender: TObject);
TFunc = function: TProcRef of object;

TMyClass = class
function GetListener: TProcRef;
end;

function TMyClass.GetListener: TProcRef;
begin< br /> Result:= procedure(Sender: TObject)
begin
Sender.Free;
end;
end;

procedure TForm1.Button1Click( Sender: TObject);
var
MyClass: TMyClass;
ProcRef: TProcRef;
Func: TFunc;

begin
MyClass:= TMyClass.Create;
// standard syntax
ProcRef:= MyClass.GetListener();

// also possible syntax
// Func:= MyClass.GetListener ;
// ProcRef:= Func();

ProcRef(MyClass);
end;

I want What to do is to assign an anonymous method as a function result to the same type of variable. Delphi complains that the appointment cannot be made. Obviously something of Delphi I want to assign the “GetListener” function instead of the result of the same function. Any help is greatly appreciated. < p>

type
TPropertyChangedListener = reference to procedure (Sender: TStimulus);

TMyClass = class
function GetListener: TPropertyChangedListener
end;


....

var MyClass: TMyClass;
Listener: TPropertyChangedListener;
begin
MyClass:= TMyClass.create;
Listener:= MyClass. GetListener; // Delphi compile error: E2010 Incompatible types: TPropertyChangedListener' and'Procedure of object'

end;

Use the following Syntax:

Listener:= MyClass.GetListener();

I wrote the following example to clarify MyClass.GetListener() and MyClass. The difference between GetListener allocation:

type
TProcRef = reference to procedure(Sender: TObject);
TFunc = function: TProcRef of object;< br />
TMyClass = class
function GetListener: TProcRef;
end;

function TMyClass.GetListener: TProcRef;
begin
Result := procedure(Sender: TObject)
begin
Sender.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject) ;
var
MyClass: TMyClass;
ProcRef: TProcRef;
Func: TFunc;

begin
MyClass:= TMyClass.Create;
// standard syntax
ProcRef:= MyClass.GetListener();

// also possible syntax
/ / Func:= MyClass.GetListener;
// ProcRef:= Func();

ProcRef(MyClass);
end;

Leave a Comment

Your email address will not be published.