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?

2
  • type TGetSomeString = function : string; // read on procedural types in the documentation Commented Mar 14, 2012 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. stackoverflow.com/questions/4626614/… Commented Mar 15, 2012 at 13:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@TLama Yes I've seen it from time to time. I have reported it: meta.stackexchange.com/questions/125587/…
@JacekKwiecień See the section named "Method Pointers" in the page that David refers to.
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.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.