Forum > Beginners

fill in procedure prototype in provided class

(1/1)

tclfpc:

Just trying to use the tdataiteratormethod of tfpdatahashtable (http://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:
It has to be a member procedure of a class, like a form for instance:

--- Code: ---  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);
...

--- End code ---

Of course you need a valid instance of the class before calling Iterate:

--- Code: ---  SomeClass := TSomeClass.Create;

--- End code ---

or use a class procedure.

--- Code: ---  TSomeClass=class
...
    class procedure YourMethod(Item: Pointer; const Key: string; var Continue: Boolean);
  end;

--- End code ---

taazz:
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: ---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;

--- End code ---
and of course you need to create before it is used

--- Code: ---var
  MyCallback : TMyCallBack;
begin
  MyCallBack := TMyCallBack.Create;
  myFPDataHashTable.Iterate := @MyCallback.DTSCallBack;
end;

--- End code ---

There a complete component and event handler.

Navigation

[0] Message Index

Go to full version