5

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?

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • `type TGetSomeString = function : string; // read on procedural types in the documentation` – OnTheFly Mar 14 '12 at 13:43
  • Typically, for methods (procedure or function that belongs to a class) you would use type `procedure(args) of object` or `function(args):resultype of object` plus the type declaration. http://stackoverflow.com/questions/4626614/delphi-please-explain-this-type-procedure-of-object – Warren P Mar 15 '12 at 13:04

2 Answers2

6

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.

NGLN
  • 43,011
  • 8
  • 105
  • 200
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

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

Community
  • 1
  • 1
Justmade
  • 1,496
  • 11
  • 16