[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

pointer of function

DELPHIer,

How can I get the pointer of a function and make use of it ?

For example,
There is a Button and some other controls in Main Form and there is a Click event in the Button object .
But, I also have three other functions called, FuncA, FuncB and FuncC inside the object of the Main Form.
So, how can I jump to FuncA or FuncB or FuncC when I click the Button through their pointers ?

thanks,
Andrew
0
andrewyu
Asked:
andrewyu
1 Solution
 
extremeCommented:
That depends on the prototype of the functions. The prototype is how the functions are declared, for example, the function for the OnClick of the Button is of type TNotifyEvent that is declared as:

TNotifyEvent = procedure (Sender : TObject) of object;

That means that TNotifyEvent is a pointer to a procedure that received a sole parameter of type TObject, and that the procedure is inside an object.

So, supposing that your functions have the same prototype (receive a sole parameter of type TObject), then you can declare in some other object three field of type TnotifyEvent. Then you can do something like this:

Form2.FuncAAddress := Form1.FuncA;
Form2.FuncBAddress := Form1.FuncB ... the same for FuncC

then you simply can call the function pointer like this:

procedure TForm2.CallFuncA;
begin
  FuncAAddress (self);
end;

that will call FuncA of the Form1 passing as parameter the self pointer. Remember that is very important to declare the function pointer with the corresponding function prototype or Delphi will refuse to compile giving you an error.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now