Re: [Lazarus] How to pass function method to TList.Sort?

2015-12-09 Thread Anthony Walter
// 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
--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


[Lazarus] How to pass function method to TList.Sort?

2015-12-09 Thread 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


--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus