Recent

Author Topic: fill in procedure prototype in provided class  (Read 2993 times)

tclfpc

  • New member
  • *
  • Posts: 5
fill in procedure prototype in provided class
« on: September 27, 2014, 04:41:57 am »

Just trying to use the tdataiteratormethod of tfpdatahashtable (**://www.freepascal.org/docs-html/fcl/contnrs/tdataiteratormethod.html). How would I have to declare a corresponding procedure to use it in iteration?

If a just declare a procedure with the same parameters as the delivered prototype and try to use it I get a compiler error in the respective call with iterate: "Error: Incompatible types: got "<address of procedure(Pointer,const AnsiString,var Boolean) is nested;Register>" expected "<procedure variable type of procedure(Pointer,const AnsiString,var Boolean) of object;Register>"

if I try to add "of object" at end procedure header, I get a syntax error: "Fatal: Syntax error, ";" expected but "OF" found".

What would be the correct way to declare and use the callback procedure?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: fill in procedure prototype in provided class
« Reply #1 on: September 27, 2014, 06:21:14 am »
It has to be a member procedure of a class, like a form for instance:
Code: [Select]
  TSomeClass=class
...
    procedure YourMethod(Item: Pointer; const Key: string; var Continue: Boolean);
  end;
...
var
  ...
  SomeClass: TSomeClass;

procedure TSomeClass.YourMethod(Item: Pointer; const Key: string;
  var Continue: Boolean);
begin
// your code
end;
...
  SomeDataHashTable.Iterate(@SomeClass.YourMethod);
...

Of course you need a valid instance of the class before calling Iterate:
Code: [Select]
  SomeClass := TSomeClass.Create;

or use a class procedure.
Code: [Select]
  TSomeClass=class
...
    class procedure YourMethod(Item: Pointer; const Key: string; var Continue: Boolean);
  end;
« Last Edit: September 27, 2014, 06:28:10 am by engkin »

taazz

  • Hero Member
  • *****
  • Posts: 5364
Re: fill in procedure prototype in provided class
« Reply #2 on: September 27, 2014, 07:30:23 am »
of object is defined when the event type was declared. That means that the call back must be a method of an object eg

Code: [Select]
TMyCallBack = class
  procedure DTSCallBack(  Item: Pointer;  const Key: string;  var Continue: Boolean);
end;

implementation

procedure TMyCallBack.DTSCallBack(  Item: Pointer;  const Key: string;  var Continue: Boolean);
begin
  //Your code here.
end;
and of course you need to create before it is used
Code: [Select]
var
  MyCallback : TMyCallBack;
begin
  MyCallBack := TMyCallBack.Create;
  myFPDataHashTable.Iterate := @MyCallback.DTSCallBack;
end;

There a complete component and event handler.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018