Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a pointer to specified function in Delphi

Not sure if the tittle is right but what I need to do is to store in some collection a pointer to specified function. I'm doing that pretty much like declaring a variable

SomeFunctionName: string

Of course this type can't be a string, the question is what should it exactly be?

like image 846
Jacek Kwiecień Avatar asked Mar 14 '12 09:03

Jacek Kwiecień


2 Answers

You would typically use a function pointer variable. For example:

type
  TProcedure = procedure;

procedure MyProc1;
begin
end;

procedure MyProc2;
begin
end;

var
  Proc: TProcedure;

.....
Proc := MyProc1;
Proc();//calls MyProc1
Proc := MyProc2;
Proc();//calls MyProc2

This is the most simple example imaginable. You can specify procedural types that have parameter list, method of object types and so on. Read more in the Procedural Types topic of the language guide.

like image 145
David Heffernan Avatar answered Sep 28 '22 10:09

David Heffernan


You are actually not storing procedure / function but storing method.

So you should use TMethod Instead. A TMethod has a object pointer and a procedure pointer.

You can see more detail in another post : Save and restore event handlers

edit : It seems the question has been edit back to original after showing some Storing TControl.onClick event request.....

like image 26
Justmade Avatar answered Sep 28 '22 11:09

Justmade