Jacek Kwiecień
Jacek Kwiecień

Reputation: 12649

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?

Upvotes: 5

Views: 2088

Answers (2)

Justmade
Justmade

Reputation: 1496

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.....

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613412

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.

Upvotes: 6

Related Questions