本人第一次使用TList,但是出现问题,大家帮我看看procedure TForm1.Button1Click(Sender: TObject);
type
TToWho=packed record //聊天对象
    Name:string[20];
    IP:string[15];
    end;
var
UserList:TList;
user:TToWho;
ip:string;
begin
userlist.create;
user.Name:='所有人';
user.IP:='192.168.0.255';
UserList.Add(@User);
ip:=TToWho(userlist.Items[0]^).IP;
showmessage(ip);
userlist.free;
end;

解决方案 »

  1.   

    var
    UserList:TList; 
    begin
      UserList:=TList.Create;
    end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    type
    TToWho=packed record //聊天对象
        Name:string[20];
        IP:string[15];
        end;
    var
    UserList:TList;
    user:TToWho;
    ip:string;
    begin
    userlist:=Tlist.create;//唉,没发现这儿出现了错误,谢谢大家了,继续散分
    user.Name:='所有人';
    user.IP:='192.168.0.255';
    UserList.Add(@User);
    ip:=TToWho(userlist.Items[0]^).IP;
    showmessage(ip);
    userlist.free;
    end;
      

  3.   

    后面的朋友再问一下,Tlist.free的时候,里面元素分配的内存,是不是自动释放了,还是需要手工释放
      

  4.   

    后面的朋友再问一下,Tlist.free的时候,里面元素用new分配的内存,是不是自动释放了,还是需要手工释放
      

  5.   

    begin
      userlist:=TList.create;
      try
        user.Name:='所有人';
        user.IP:='192.168.0.255';
        UserList.Add(@User);
        ip:=TToWho(userlist.Items[0]^).IP;
        showmessage(ip);
      finally
        userlist.free;
      end;
    end;
      

  6.   

    借小和同志的问题再深入一下^_^
    type 
     PToWho=^TToWho;//指针
     TToWho=packed record //聊天对象 
        Name:string[20]; 
        IP:string[15]; 
        end; 
     ....
     Puser:PToWho;
     new(Puser);
     .....
     UserList.Add(Puser);
     
    procedure TForm1.Button1Click(Sender: TObject); 
    var
      I:Integer;
      Puser:PToWho;
    begin
      for i:=0 to UserList.count-1 do
      begin
        Puser:=UserList.Items[I];
        TreeView.addobject(nil,Puser.name,Puser);
        UserList.Delete(I);
        接下来还需要Dispose(Puser)吗?
        因为Puser指针绑到TreeView上了,如果Dispose的话TreeView上的指针引用还有效吗?
      end;
    end;
      

  7.   

    刚才的一句应该这样写
      for i:=UserList.count-1 downto 0 do
    不然肯定会出错
      

  8.   

    后面的朋友再问一下,Tlist.free的时候,里面元素分配的内存,是不是自动释放了,还是需要手工释放
    ---------------------------------------------------------
    个人观点:TList是一个数组,里面仅存放对象的指针,所以在释放TList时,需要先将List列表中的对象释放掉。故它不太适合放对象。正因为这些,Delphi从TList类继承了一个TObjectList,该类也是个容器,其是有个OwnsObjects属性来管理列表内部对象的生存周期!
      

  9.   

    //TList的删除
    procedure TList.Delete(Index: Integer);
    var
      Temp: Pointer;
    begin
      if (Index < 0) or (Index >= FCount) then
        Error(@SListIndexError, Index);
      Temp := Items[Index];
      Dec(FCount);
      if Index < FCount then
        System.Move(FList^[Index + 1], FList^[Index],
          (FCount - Index) * SizeOf(Pointer));
      if Temp <> nil then
        Notify(Temp, lnDeleted);//调用ObjectList的Notify
    end;ObjectList的Notify
    procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
    begin
      if OwnsObjects then
        if Action = lnDeleted then
          TObject(Ptr).Free;
      inherited Notify(Ptr, Action);
    end;
      

  10.   

    而List本身的Notify没有实现,故不能管理对象周期
      

  11.   

    虽然是接分,但是问题还是要指出:
    procedure TForm1.Button1Click(Sender: TObject);
    type
      PToWho = ^TToWho;
      TToWho = packed record //聊天对象
        Name: string[20];
        IP: string[15];
      end;
    var
      UserList: TList;
      //user: TToWho;
      user: PToWho;
      i: Integer;
      ip: string;
    begin
      userlist := TList.create;
      try
        //如下代码很少这样用
        {user.Name := '所有人';
        user.IP := '192.168.0.255';
        UserList.Add(@User);}    //一般这样写,比方说加3个,再显示
        for i := 1 to 3 do
        begin
          New(User);
          User^.IP := '192.168.0.' + IntToStr(i);
          User^.Name := '所有人' + IntToStr(i);
          UserList.Add(user);
        end;    for i := 0 to UserList.Count - 1 do
        begin
          ip := PToWho(userlist.Items[i])^.IP;
          ShowMessage(ip);
        end;    //ip := TToWho(userlist.Items[0]^).IP;
        //showmessage(ip);
      finally
        for i := 0 to UserList.Count - 1 do
          Dispose(UserList[i]); //释放UserList中的指针
        userlist.free;
      end;
    end
      

  12.   

    以前用的也是26楼的方法,我看过liwei的书,18楼提醒了我,让我记起了TObjectList了,delphi社区中还是有一帮认真在学习和研究的人。大家老讨论编程语言和工具什么的,其实没有什么多大意义。
    我就经常把编程和武术联接起来,武术的套路派数多,各有各的优点,只要把某一种武术练到了极点,一样可以敌克名门正派,武术大家,武林中卧虎藏龙,不一不定都是些有名的人物。软件编程也一样,高手不一定都在正规公司,大公司,不喜欢虚名争权夺势,隐姓埋名的大在,偶尔在社区漂流一下,启不更逍遥快活。。