如何将记录类型的记录添加到 TList<> 中?

发布于 2024-11-18 18:00:42 字数 2119 浏览 3 评论 0 原文

我有一个数据树列表。我循环遍历树列表以匹配某些记录并将它们添加到通用 TList<> 中。除非所有记录值都成为 TList 中所有项目的最后一个添加值,否则此操作有效。

这是一些代码:

type
  TCompInfo = record
  private
    class var
      FCompanyName    : string;
      FCompanyPath    : string;
      FCompanyDataPath: string;
      FCompanyVer     : string;
  public
    class procedure Clear; static;
    class property CompanyName : string read FCompanyName write FCompanyName;
    class property CompanyPath : string read FCompanyPath write FCompanyPath;
    class property CompanyDataPath : string read FCompanyDataPath write FCompanyDataPath;
    class property CompanyVer : string read FCompanyVer write FCompanyVer;
  end;
  TCompList = TList<TCompInfo>;

// variablies defined ...
var
  CompData : TCompData;
  AList : TCompList;

像这样添加记录:

  tlCompanyList.GotoBOF;
  for i := 0 to tlCompanyList.Count-1 do
  begin
    if colCompanyChecked.Value then
    begin
      inc(ItemsChecked);
      CompData.CompanyName := colCompanyName.Value;
      CompData.CompanyDataPath := colCompanyDataPath.Value;
      CompData.CompanyPath := colCompanyPath.Value;
      CompData.CompanyVer := colCompanyVersion.Value;
      AList.Add(CompData);
  end;
  tlCompanyList.GotoNext;

...或像这样添加记录:

  tlCompanyList.GotoBOF;
  for i := 0 to tlCompanyList.Count-1 do
  begin
    if colCompanyChecked.Value then
    begin
      inc(ItemsChecked);
      AList.Count := ItemsChecked;
      AList.Items[ItemsChecked-1].CompanyName := colCompanyName.Value;
      AList.Items[ItemsChecked-1].CompanyDataPath := colCompanyDataPath.Value;
      AList.Items[ItemsChecked-1].CompanyPath := colCompanyPath.Value;
      AList.Items[ItemsChecked-1].CompanyVer := colCompanyVersion.Value;
  end;
  tlCompanyList.GotoNext;

会产生完全相同的结果。 AList.Items[0...Count-1] 都具有相同的值。单步执行代码,我可以看到正在捕获正确的数据,但是一旦我将新记录保存到 AList,所有以前的记录都会采用相同的值。这表明 TList 中的每个项目都是指向内存中同一记录的指针。如果记录所占用的内存发生变化,所有项目都会发生变化。这使得但不是我想要的。如何在 TList 中分配新记录来保存不同的数据?

我知道我可以通过其他方式实现最终结果,而且我确实做到了。对于现在使用泛型和记录的我来说,这已经变得更具教育意义。我正在使用Delphi XE。

谢谢

I have a treelist of data. I'm looping through the tree list to match certain records and adding them to a generic TList<>. This works except all record values become the last one added for all items in the TList.

Here's some code:

type
  TCompInfo = record
  private
    class var
      FCompanyName    : string;
      FCompanyPath    : string;
      FCompanyDataPath: string;
      FCompanyVer     : string;
  public
    class procedure Clear; static;
    class property CompanyName : string read FCompanyName write FCompanyName;
    class property CompanyPath : string read FCompanyPath write FCompanyPath;
    class property CompanyDataPath : string read FCompanyDataPath write FCompanyDataPath;
    class property CompanyVer : string read FCompanyVer write FCompanyVer;
  end;
  TCompList = TList<TCompInfo>;

// variablies defined ...
var
  CompData : TCompData;
  AList : TCompList;

Adding records like this:

  tlCompanyList.GotoBOF;
  for i := 0 to tlCompanyList.Count-1 do
  begin
    if colCompanyChecked.Value then
    begin
      inc(ItemsChecked);
      CompData.CompanyName := colCompanyName.Value;
      CompData.CompanyDataPath := colCompanyDataPath.Value;
      CompData.CompanyPath := colCompanyPath.Value;
      CompData.CompanyVer := colCompanyVersion.Value;
      AList.Add(CompData);
  end;
  tlCompanyList.GotoNext;

...or adding records like this:

  tlCompanyList.GotoBOF;
  for i := 0 to tlCompanyList.Count-1 do
  begin
    if colCompanyChecked.Value then
    begin
      inc(ItemsChecked);
      AList.Count := ItemsChecked;
      AList.Items[ItemsChecked-1].CompanyName := colCompanyName.Value;
      AList.Items[ItemsChecked-1].CompanyDataPath := colCompanyDataPath.Value;
      AList.Items[ItemsChecked-1].CompanyPath := colCompanyPath.Value;
      AList.Items[ItemsChecked-1].CompanyVer := colCompanyVersion.Value;
  end;
  tlCompanyList.GotoNext;

Results in the exact same thing. AList.Items[0...Count-1] all have the same values. Stepping through the code I can see the correct data is being captured but once I save a new record to AList all previous records take on the same values. This shows me that each item in the TList is a pointer to the same record in memory. If the memory the record is taking changes all items will change. This make since but is not what I want. How do I allocate for new records in a TList to hold different data?

I know I can accomplish the end result in other ways and indeed I have. This has become more of an educational thing for me now using generics and records. I am using Delphi XE.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我不在是我 2024-11-25 18:00:42

您已将记录的所有字段声明为“class var”。在类中,“class var”的值对于该类的所有实例都是相同的。实际上,我从未对记录使用过“class var”,但我猜想记录类型的语义也是相同的。这意味着每当您更改一条记录中的字段值时,所有现有记录中的字段值都会发生更改。

尝试不使用“class var”和简单的“property”而不是“class property”。

You have declared all the fields of the record as "class var". In a class a "class var"'s value is the same for all instances of the class. Actually I never used "class var" with records, but I guess that the semantic is the same for record types too. Meaning that whenever you change the value of the field in one record, it will change in all existent records.

Try it without "class var" and simple "property" instead of "class property".

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