[fpc-pascal] how to pass a procedure address and execute it?

Ewald ewald at yellowcouch.org
Fri Jan 24 22:54:18 CET 2014


On 24 Jan 2014, at 22:20, waldo kitty wrote:

> On 1/24/2014 3:18 PM, Ewald wrote:
>> 
>> On 24 Jan 2014, at 21:20, waldo kitty wrote:
>> 
>>> On 1/23/2014 2:18 PM, waldo kitty wrote:
>>> 
>>> following up on this, how do i pass parameters to doThis and doThat?? do i
>>> have to use an intermediate pre_doThis and pre_doThat which handles the calls
>>> from centralControl and then calls doThis and doThat with the necessary parameters?
>>> 
>> 
>> Simply change the type:
>> 	Type
>> 		TProc = Procedure(aRecord: somerec);
>> 
>> Then you can call whichProc (in centralControl) with your argument of choice.
> 
> ahhh! that helps to explain what the compiler error was that i was seeing... so i may need two different types if each procedure called from centralControl has different parameters?

Basically yes (if you mean different parameter types/ different parameter order -- the name in the procedure/function type is merely descriptive AFAIK). But if you have many procedures, I'd go for a class:

TCentralControl = Class
Protected
	Procedure Callback1; virtual; abstract;
	Procedure Callback2(value: TSomeRec); virtual; abstract;
	Function Callback3: Boolean; virtual abstract;
	... and so on ...	

Public
	Procedure CentralControl(.....);
End;

Then simply inherit from this class a few times for the different scenario's you want to cover.


> won't that cause a conflict if the parameters are not the same or are in different ordering or even if some procedure doesn't have parameters at all?


Yes, you would need a lot of parameters if you need a lot of callbacks. if this is the case go for the OOP approach; it is perfectly suited for this kind of thing.

--
Ewald




More information about the fpc-pascal mailing list