[Lazarus] *** SPAM *** How to pass function method to TList.Sort?
Howard Page-Clark
hdpc at talktalk.net
Wed Dec 9 22:38:12 CET 2015
On 09/12/2015 20:11, Donald Ziesig wrote:
> 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;
objectFunction:=@Compare;
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
More information about the Lazarus
mailing list