我可以直接将记录添加为字符串列表中的对象吗?
目前,我通过创建对象来添加对象,如下所示:
type
TRecord = class
private
str: string;
num: Integer;
public
constructor Create;
end;
...
procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
rec: TRecord;
Alist: TStringList;
begin
Alist := TStringList.create;
Alist.Clear;
for i := 0 to 9 do
begin
rec := Trecord.Create; //create instance of class
rec.str := 'rec' + IntToStr(i);
rec.num := i * 2;
Alist.AddObject(IntToStr(i), rec);
end;
end;
此方法正确还是效率低下? 或者我可以直接添加对象而不是像使用记录一样创建它吗?
type
PRec = ^TRec;
TRec = record
str: string;
num: Integer;
end;
...
var
rec: TRec;
...
for i := 0 to 9 do
begin
//how to write here to have a new record,
//can i directly Create record in delphi 7 ?
rec.str := 'rec' + IntToStr(i);
rec.num := i*2;
Alist.AddObject(IntToStr(i), ???); // how to write here?
end;
或者其他快速简单的方法?
我正在使用 Delphi 7。
提前致谢。
Currently I am adding object by creating it like:
type
TRecord = class
private
str: string;
num: Integer;
public
constructor Create;
end;
...
procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
rec: TRecord;
Alist: TStringList;
begin
Alist := TStringList.create;
Alist.Clear;
for i := 0 to 9 do
begin
rec := Trecord.Create; //create instance of class
rec.str := 'rec' + IntToStr(i);
rec.num := i * 2;
Alist.AddObject(IntToStr(i), rec);
end;
end;
Is this method correct or inefficient ?
Or Can I directly add object not by creating it like using record?
type
PRec = ^TRec;
TRec = record
str: string;
num: Integer;
end;
...
var
rec: TRec;
...
for i := 0 to 9 do
begin
//how to write here to have a new record,
//can i directly Create record in delphi 7 ?
rec.str := 'rec' + IntToStr(i);
rec.num := i*2;
Alist.AddObject(IntToStr(i), ???); // how to write here?
end;
Or other fast and simple way?
I am using Delphi 7.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你现在的做法没问题。
当您向
TStringList.Objects
添加新记录时,如果不分配内存,就无法对记录执行此操作,并且之后必须释放它。你和现在一样使用类;您必须在释放字符串列表之前释放对象。 (在较新版本的 Delphi 中,TStringList
有一个OwnsObjects
属性,当字符串列表被释放时,该属性会自动释放它们,但在 Delphi 7 中没有。 )如果您确实想对记录执行此操作,您可以:
您需要使用 System.Dispose(PRec(AList.Objects[i])) 来在释放字符串列表之前释放内存。正如我所说,你现在的做法实际上要容易得多;在字符串列表中添加和删除时不必进行类型转换。
顺便说一句,您不需要
AList.Clear
。由于您正在创建字符串列表,因此其中不能删除任何内容。The way you're doing it now is fine.
You can't do it with a record without allocating memory when you add a new record to the
TStringList.Objects
, and you'd have to free it afterwards. You're just as well off using a class as you are now; you have to free the objects before freeing the stringlist. (In more recent versions of Delphi,TStringList
has anOwnsObjects
property that will auto-free them for you when the stringlist is free'd, but it's not in Delphi 7.)If you really want to do this with a record, you can:
You'll need to use
System.Dispose(PRec(AList.Objects[i]))
to release the memory before freeing the stringlist. As I said, the way you're doing it now is actually much easier; you don't have to do the typecast when adding to and deleting from the stringlist.You don't need the
AList.Clear
, by the way. Since you're creating the stringlist, there can't be anything in it to remove.