我可以直接将记录添加为字符串列表中的对象吗?

发布于 2025-01-02 04:27:12 字数 999 浏览 1 评论 0原文

目前,我通过创建对象来添加对象,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2025-01-09 04:27:12

你现在的做法没问题。

当您向 TStringList.Objects 添加新记录时,如果不分配内存,就无法对记录执行此操作,并且之后必须释放它。你和现在一样使用类;您必须在释放字符串列表之前释放对象。 (在较新版本的 Delphi 中,TStringList 有一个 OwnsObjects 属性,当字符串列表被释放时,该属性会自动释放它们,但在 Delphi 7 中没有。 )

如果您确实想对记录执行此操作,您可以:

type    
  PRec = ^TRec;
  TRec = record
    str: string;
    num: Integer;
  end;

var
  rec: PRec;
begin
  for i := 0 to 9 do 
  begin
    System.New(Rec);
    rec.str := 'rec' + IntToStr(i);
    rec.num := i*2;
    Alist.AddObject(IntToStr(i), TObject(Rec)); // how to write here?
  end;
end;

您需要使用 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 an OwnsObjects 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:

type    
  PRec = ^TRec;
  TRec = record
    str: string;
    num: Integer;
  end;

var
  rec: PRec;
begin
  for i := 0 to 9 do 
  begin
    System.New(Rec);
    rec.str := 'rec' + IntToStr(i);
    rec.num := i*2;
    Alist.AddObject(IntToStr(i), TObject(Rec)); // how to write here?
  end;
end;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文