PDA

View Full Version : Property pointing to function



SilverWarior
25-01-2012, 07:50 PM
Hi guys!
How to create a property wich points to certain function in FPC.
In Delphi you declare it like this:

TSomeObject = class(TObject)
type
TSomeFunction = function (Var: Integer): Integer of object;
private
FSomeFunction: TSomeFunction;
public
property SomeFunction: TSomeFunction read FSomeFunction write FSomeFunction;
end;
//Globaly accesible function
function SomeExistingFunctionInCode(Var: Integer): Integer;

And you set it like this:

SomeFunction := SomeExistingFunctionInCode;

This doesn't work in FPC/Lazarus. I get Error: Wrong number of parameters specified to call to "SomeFunction"

User137
25-01-2012, 08:10 PM
In fpc you assign procedudes with @


SomeFunction := @SomeExistingFunctionInCode;

Also, i doubt that it compiles in Delphi either like that. If you have a global procedure it must be fixed in type definition:

TSomeFunction = function (Var: Integer): Integer; // remove "of object"
// that can only be used if the function is defined in a class, such as TForm, TObject or any other

SilverWarior
25-01-2012, 08:48 PM
Thanks for your quick reply!
The code compiles and works in Delphi perfectly as I have writen it in Delphi first. Now I'm trying to make it also compatible with FPC .I hope this is the last adjustment to the code I'll have to made so I can finaly finish my article.