Can't call Binarysearch function on TObjectList

If we look at the XE2 or XE3 online help for TObjectList methods , we can see that the binarysearch function is available for TObjectList. But if we try to use XE3, it won't even compile.

In this example, the sorting function is also available, but it compiles.

Any idea is appreciated.

Sample code:

unit FM_Main;

  interface

  uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Contnrs, Vcl.CheckLst, System.Generics.Collections;

  type
     TTPRODData = class
     private
        FData1 : String;
        FData2 : String;

        FCount : Integer;
     public
        constructor Create; overload;
        destructor Destroy; override;
     end;

     TTPRODDataList = class(TObjectList)

        function GetItem(Index: Integer): TTPRODData;
        procedure SetItem(Index: Integer; const Value: TTPRODData);
     public
        constructor Create; overload;
        destructor  Destroy; override;

        property Items[Index: Integer]: TTPRODData read GetItem write SetItem; default;
        procedure SortOnProductCode;

     end;

    TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    private
      { Private declarations }
    public
      { Public declarations }
    end;

  var
    Form1: TForm1;

  implementation

  {$R *.dfm}

  //
  // Sort function.
  //
  function CompareProductCode(Item1, Item2: Pointer): Integer;
  begin
     Result := CompareStr(TTPRODData(Item1).FData1, TTPRODData(Item2).FData1);
  end;

  //
  //
  //

  procedure TForm1.Button1Click(Sender: TObject);
  var
     aProdList : TTPRODDataList;
     aDummy : TTPRODData;
     aNdx : Integer;

  begin
     aProdList := TTPRODDataList.Create;

     // This call works.
     aProdList.Sort(CompareProductCode);

     // This call doesn't even compile !
     aProdList.BinarySearch(aDummy, aNdx);
  end;

  { TTPRODData }

  constructor TTPRODData.Create;
  begin
     inherited Create;

     FData1 := '';
     FData2 := '';
     FCount := 0;
  end;

  destructor TTPRODData.Destroy;
  begin
    inherited;
  end;

  { TTPRODDataList }

  constructor TTPRODDataList.Create;
  begin
     inherited Create;
  end;

  destructor TTPRODDataList.Destroy;
  begin
     Clear;

     inherited;
  end;

  function TTPRODDataList.GetItem(Index: Integer): TTPRODData;
  begin
     result := TTPRODData(inherited GetItem(index));
  end;

  procedure TTPRODDataList.SetItem(Index: Integer; const Value: TTPRODData);
  begin
     inherited setItem(index, value);
  end;

  procedure TTPRODDataList.SortOnProductCode;
  begin
     Sort(CompareProductCode);
  end;

  end.

      


As David Heffernan suggested, follow the comparison code for the sort function.

For those interested, follow the code for the comparison method:

TTProdComparer = class(TComparer<TTPRODData>)
public
   function Compare(const Item1, Item2: TTPRODData): Integer; override;
end;

And the code :
{ TTProdComparer }
function TTProdComparer.Compare(const Item1, Item2: TTPRODData): Integer;
begin
   Result := CompareStr(Item1.FData1 , Item2.FData1 );
end;

      

+3


source to share


1 answer


The documentation you're linking to is for a generic container TObjectList<T>

from a block Generics.Collections

.

But the class you used in your code is a legacy non-shared container TObjectList

from a block Contnrs

.

The method BinarySearch

you are trying to use only exists in the generic class.

If you switch to a shared container, you will find that you can remove most of the boiler plate code from your class. It will be:



TTPRODDataList = class(TObjectList<TTPRODData>)
public
  procedure SortOnProductCode;
end;

      

You don't need GetItem

, SetItem

and Items

, because the generic type class already has this functionality.

The only work you have to do is adapt your sorting code to accommodate several of the different interfaces used in common Delphi containers.

+7


source







All Articles