Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-12

How to pass function method to TList.Sort?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Donald Ziesig

Hi All!

Is there any way to pass a method function as the Compare parameter to a TList or is there a work around for it?

There are generics involved so it won't let me access the static symbol table.

As in:

type
   TMyClass = object
      fIndex : Integer;
      List : array of TList;
      function Compare( Item1, Item2 : Pointer) : Longint; // needs
access to fIndex
      procedure Sort;
   end;
implementation
function TMyClass.Compare( Item1, Item2 : Pointer ) : Longint;
begin
    case fIndex of
       0:  Result := some compare of Item1, Item2;// These compares are
implemented in an object from another unit
       1:  Result := another compare of Item1, Item2
    end;
end;
procedure TMyClass.Sort;
begin
   fIndex := 0;
   List[fIndex].Sort( @Compare );  // Compilation error, doesn't like
Compare being "of object"
end;

Thanks,

Don Ziesig

Reply by email

Anthony Walter
to Donald Ziesig
// Drop the TMyClass.
function Compare( Item1, Item2 : Pointer ) : Longint;
begin
   case fIndex of
      0:  Result := some compare of Item1, Item2;// These compares are
implemented in an object from another unit
      1:  Result := another compare of Item1, Item2
   end;
end;

Sort takes a function, not a method. There is a difference. A function pointer is, well one pointer. A method pointer (function/procedure of object) is tow pointers 1st pointer) for the function 2nd pointer) for the object instance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151209/d834aab5/attachment.html>

Reply by email

Howard Page-Clark
to Donald Ziesig: Hi All! Is there any way to pass a method function as the Compare parameter to a TList or…
Donald Ziesig wrote 31 lines
Is there any way to pass a method function as the Compare parameter to a TList or is there a work around for it? As in: type TMyClass = object fIndex : Integer; List : array of TList; function Compare( Item1, Item2 : Pointer) : Longint; // needs access to fIndex procedure Sort; end; implementation function TMyClass.Compare( Item1, Item2 : Pointer ) : Longint; begin case fIndex of 0: Result := some compare of Item1, Item2;// These compares are implemented in an object from another unit 1: Result := another compare of Item1, Item2 end; end; procedure TMyClass.Sort; begin fIndex := 0; List[fIndex].Sort( @Compare ); // Compilation error, doesn't like Compare being "of object" end;

It's a dreadful hack, but you could try something like:

type
   TClassListSortCompare = function (Item1, Item2: Pointer): Integer of
object;
procedure TMyClass.Sort;
var
   objectFunction: TClassListSortCompare;
   regularFunction: TListSortCompare;
begin
   fIndex := 0;
   ;
   regularFunction:=TListSortCompare(TMethod(objectFunction).Code);
   List[fIndex].Sort( regularFunction );
end;

---
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Reply by email