Results 1 to 3 of 3

Thread: Property pointing to function

  1. #1

    Property pointing to function

    Hi guys!
    How to create a property wich points to certain function in FPC.
    In Delphi you declare it like this:
    Code:
    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:
    Code:
    SomeFunction := SomeExistingFunctionInCode;
    This doesn't work in FPC/Lazarus. I get Error: Wrong number of parameters specified to call to "SomeFunction"

  2. #2
    In fpc you assign procedudes with @

    Code:
    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:
    Code:
    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

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •