本人第一次使用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;
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;
解决方案 »
- 关于Access中“同步复制ID”类型的问题???
- 如何將一個字符串定義成一個全局變量?
- 有多种商品, 属性不同, 是应该每一种商品建一个表还是允许全部建在一起?(我只有20分了)
- BlockWrite函数的用法(关于I/O Error)
- 添加,修改,删除记录的问题!各位帮忙,多谢
- delphi连接sql2000的问题
- 寻找淄博张店的难友,大家一块交流一下
- 【十万火急:Dbexpress问题】 SQLStoredProc调用oracle *包*中存储过程的问题 报错找不到数据???
- 超难SQL问题请教,实在是想不出来。
- 求救!!!!!delphi 控制数码像机(网眼pc350支持twain)
- 点击查询按钮后,出现表,而且自动在表后加一个计算的列,怎么做?
- 一个delphi的初级问题
UserList:TList;
begin
UserList:=TList.Create;
end;
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;
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;
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;
for i:=UserList.count-1 downto 0 do
不然肯定会出错
---------------------------------------------------------
个人观点:TList是一个数组,里面仅存放对象的指针,所以在释放TList时,需要先将List列表中的对象释放掉。故它不太适合放对象。正因为这些,Delphi从TList类继承了一个TObjectList,该类也是个容器,其是有个OwnsObjects属性来管理列表内部对象的生存周期!
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;
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
我就经常把编程和武术联接起来,武术的套路派数多,各有各的优点,只要把某一种武术练到了极点,一样可以敌克名门正派,武术大家,武林中卧虎藏龙,不一不定都是些有名的人物。软件编程也一样,高手不一定都在正规公司,大公司,不喜欢虚名争权夺势,隐姓埋名的大在,偶尔在社区漂流一下,启不更逍遥快活。。