Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 无法为TObjectList调用Binarysearch函数_Delphi_Tobjectlist - Fatal编程技术网

Delphi 无法为TObjectList调用Binarysearch函数

Delphi 无法为TObjectList调用Binarysearch函数,delphi,tobjectlist,Delphi,Tobjectlist,如果我们查看XE2或XE3的联机帮助 ,我们看到TObjectList可以访问binarysearch函数。但如果我们尝试使用XE3,它甚至无法编译 例如,sort函数也是可用的,但是这个函数是可编译的 任何想法都欢迎 示例代码: unit FM_Main; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics

如果我们查看XE2或XE3的联机帮助 ,我们看到TObjectList可以访问binarysearch函数。但如果我们尝试使用XE3,它甚至无法编译

例如,sort函数也是可用的,但是这个函数是可编译的

任何想法都欢迎

示例代码:

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.

正如David Heffernan所建议的,下面是排序函数的比较器代码

对于感兴趣的人,请按照比较器方法的代码进行操作:

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;
TTProdComparer=class(TComparer)
公众的
函数比较(const Item1,Item2:TTPRODData):整数;推翻
结束;
以及守则:
{TTProdComparer}
函数TTProdComparer.Compare(const Item1,Item2:TTPRODData):整数;
开始
结果:=比较(第1.FData1项,第2.FData1项);
结束;

您链接到的文档是来自
Generics.Collections
单元的通用容器
TObjectList

但是您在代码中使用的类是
Contnrs
单元中的遗留非通用容器
TObjectList

您尝试使用的
BinarySearch
方法仅存在于泛型类中

如果您切换到通用容器,那么您会发现您可以从类中删除大多数锅炉板代码。它变成:

TTPRODDataList = class(TObjectList<TTPRODData>)
public
  procedure SortOnProductCode;
end;
TTPRODDataList=class(TObjectList)
公众的
程序SortOnProductCode;
结束;
您不需要
GetItem
SetItem
Items
,因为类型安全泛型类已经对该功能进行了排序

您所要做的唯一工作就是调整排序代码,以适应Delphi通用容器使用的略有不同的接口