Plocationrecord是我自己定义的一个结构体
type
  Plocationrecord=^_locarecord;     // 将行号和id 存放一起
    _locarecord=record 
    lowrow:integer;
    highrow:integer;
    firendid:integer;              //放id
  End;
procedure TForm1.Button2Click(Sender: TObject);
var location:Plocationrecord;
begin
   new(location);
   location.lowrow:=1;
   location.highrow:=11;
   location.firendid:=111;
   if locationList.IndexOf(location)=-1 then//这里的location值不都是一样吗?为什么跟着显示的不一样呢?
         locationList.Add(location);
   ShowMessage(IntToStr(locationList.Count)); 
end;
我的问题就是IndexOf如何检测结构体呢?因为每次单击button,它的Count就加1

解决方案 »

  1.   

    是不是因为我new(location);然后它的地址空间不同了啊?
    那我想判断某一个结构体是否已经加入?
    是不是只有每个变量都循环判断一次?这样做太麻烦了吧
      

  2.   

    new(location);
    新分配了一个location 地址肯定和以前不一样
    locationList.IndexOf(location)=-1 永远都是-1
      

  3.   

    locationList.IndexOf(location) 这个是根据 location的地址来获取index不是对它下面的变量做比较的。locationList.IndexOf(location) 在你语句里没有什么实际意义
      

  4.   


    function TList.IndexOf(Item: Pointer): Integer;
    begin
      Result := 0;
      while (Result < FCount) and (FList^[Result] <> Item) do
        Inc(Result);
      if Result = FCount then
        Result := -1;
    end;
    贴上源码,原来FList^[Result] 根据地址来比较的