Domanda

What am I doign wrong with the following code

function CompareFloat(List: TStringList; Index1, Index2: Integer): Integer;

and I call it as :

var
   SL :TstringList;

SL.CustomSort(CompareFloat);
//SL.CustomSort(@CompareFloat); // Tried this one also 

The first function call 'SL.CustomSort(CompareFloat)' retrieves that error from compiler "Error: Wrong number of parameters specified for call to "CompareFloat"

Second function call 'SL.CustomSort(@CompareFloat)' retrieves that error from compiler Error: Only class methods can be referred with class references

È stato utile?

Soluzione

SL.CustomSort(CompareFloat); works if you add {$mode delphi} directive to somewhere to the beginning of a unit.

However SL.CustomSort(@CompareFloat); should work fine. Make sure the error message is not caused by something else.

Example:

program Project1;

//{$mode delphi}

uses
  Classes,
  SysUtils;

function CompareFloat(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrToInt(List[Index1]) - StrToInt(List[Index2]);
end;

var
  SL: TStringList;
begin
  SL := TStringList.Create;
  try
    SL.Add('3');
    SL.Add('2');
    SL.Add('1');
    SL.CustomSort(@CompareFloat);
    //SL.CustomSort(CompareFloat);
    Writeln(SL[0], SL[1], SL[2]);
    Readln;
  finally
    SL.Free;
  end;
end.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top