问题可能是:每次在调用 AddObject 之前,是否使用 New 过程为新的指针申请了内存空间?
申请之后再给 Handle 赋值,然后再插入。TStringList 实际插入的是一个地址而已。
已经有现成的 TCollection 了,为什么要这样一个新的类呢?要是不满意 TCollection 
的话,可以继承一下,然后改一改嘛!代码重用是很重要的。

解决方案 »

  1.   

    使用New了。主要是不知道为什么,使用TCollection 会出现非法错误。
      

  2.   

    所有的OBject都有一个Tag是longint的你可以拿来随便用
    另外你的AddObject操作之前都要分配个xp要不然操作的是同一个XP我看了你好像要存储的结构是一个字符串和一个数值那么你可以用
    ct.Names[String]:=IntToStr(intvalue);
    要取值时用
    intValue:=StrToInt(ct.Names[TreeView1.Selected.Text]);
    这样就避免了要动态创建对象,分配空间,释放空间增加错误的可能
      

  3.   

    经过测试没有问题,可能是你的下标不对应type
    PMyRec = ^TMyRec;
    TMyRec = record
      Handle: Int64;
    end;var
      xp:PMyRec;
      ct:tstringlist;
    begin
      ct:=tstringlist.Create;
      new(xp);
      xp.Handle :=1234;
      ct.AddObject('ab',TObject(xp));
      ShowMessage(inttostr((pmyrec(ct.Objects[0]))^.Handle));
      FreeAndNil(ct);
    end;
      

  4.   

    to weenyboy(小公子) 
    我也觉得奇怪,代码没有改变,昨天还是测试通过的。另外Handle会是一个很大的值,所以
    才会用Int64
      

  5.   

    自己写一个类来搞定一切吧;以下代码没有经过测试 TRec=class
      private
        Flargeint:array of int64;
        Fs:array of string;
        Fcount:integer;
        function getlargeint(index: integer): int64;
        function gets(index: integer): string;
      public
        constructor Create;virtual;
        procedure Add(s:string;i:int64);
        property LargeInt[index:integer]:int64 read getlargeint;
        property S[index:integer]:string read gets;
      end;implementation{ TRec }procedure TRec.Add(s: string; i: int64);
    begin
      flargeint[fcount]:=i;
      fs[fcount]:=s;
      inc(fcount);
      setlength(flargeint,fcount);
      setlength(fs,fcount);
    end;constructor TRec.Create;
    begin
      fcount:=0;
      flargeint:=vararraycreate([0,fcount],varvariant);
      fs:=vararraycreate([0,fcount],varvariant);
    end;function TRec.getlargeint(index: integer): int64;
    begin
      if index<fcount then
         result:=flargeint[index];
    end;function TRec.gets(index: integer): string;
    begin
      if index<fcount then
        result:=fs[index];
    end;
      

  6.   

    to weenyboy(小公子) 
    谢谢你的代码,但是我需要能够通过String来定位而不只是索引。实际上我写了两个代码,
    但是在通过字符串查找、定位方面的速度成问题,因为数据量比较大。
      

  7.   

    既然要用TStringList那就要充分的用,TStringList有很强大的功能的。TStringlist和TStrings是我见过最有意义的控件。
    用names,CommaText吧,那几乎就能解决大部分问题不够再加上Objects