在 Delphi TDictionary 中,我的 Value 对象作为 null 插入

发布于 2024-09-14 13:50:23 字数 1803 浏览 8 评论 0原文

我正在使用 Delphi 9 的 TDictionary 泛型类。我的 TDictionary 看起来像这样:

g_FileHandlers : TDictionary<String, TList<String>>;

并且,所以我像这样初始化 TDictionary:

g_FileHandlers := TDictionary<String, TList<String>>.Create;

我有一个 TList,我也在初始化它,以便我可以使用它来填充 TDictionary。我正在循环遍历一些用于填充 TList/TDictionary 的文件数据,并且尝试重新使用相同的 TList 作为值插入到 TDictionary 中。第一次插入 TDictionary 时,该项目的 TList 值存在并且其中包含数据。在第二次和后续迭代中,TList 值均为零。

g_FilePaths := TList<String>.Create;

在我看来,这一切都是通过参考来完成的。如何按值(而不是引用)将 TList 添加到我的 TDictionary?

  // Create our dictionary of files and handlers
  for i := 0 to g_FileList.Count - 1 do
  begin
    g_HandlerName := AnsiMidStr(g_FileList[i], 2, Length(g_FileList[i]));
    g_HandlerName := AnsiMidStr(g_HandlerName, 1, Pos('"', g_HandlerName) - 1);

    if i = 0 then
      g_PreviousHandlerName := g_HandlerName;

    if AnsiCompareText(g_HandlerName, g_PreviousHandlerName) = 0 then
    begin
      g_FilePath := AnsiMidStr(g_FileList[i], Length(g_HandlerName) + 5, Length(g_FileList[i]));
      g_FilePath := AnsiMidStr(g_FilePath, 1, Length(g_FilePath) - 1);
      g_FilePaths.Add(g_FilePath);
    end
    else
    begin
      g_FileHandlers.Add(g_PreviousHandlerName, g_FilePaths);

      g_FilePaths.Clear;
      g_FilePath := AnsiMidStr(g_FileList[i], Length(g_HandlerName) + 5, Length(g_FileList[i]));
      g_FilePath := AnsiMidStr(g_FilePath, 1, Length(g_FilePath) - 1);
      g_FilePaths.Add(g_FilePath);
    end;

    if AnsiCompareText(g_HandlerName, g_PreviousHandlerName) <> 0 then
      g_PreviousHandlerName := g_HandlerName;

    if i = g_FileList.Count - 1 then
      g_FileHandlers.Add(g_HandlerName, g_FilePaths);
  end;
  g_FilePaths.Free;

I'm using Delphi 9's TDictionary generic class. My TDictionary looks like this:

g_FileHandlers : TDictionary<String, TList<String>>;

And, so I initialize the TDictionary like so:

g_FileHandlers := TDictionary<String, TList<String>>.Create;

I have a TList, that I'm also initializing, so that I can use it to populate the TDictionary. I'm looping over some file data which I use to populate the TList/TDictionary, and I'm trying to re-use the same TList to insert into the TDictionary as the Value. On the first insertion into the TDictionary, the TList value of the item exists and has the data in it. On the second, and subsequent iterations though the TList Values are all nil.

g_FilePaths := TList<String>.Create;

It sounds to me like it's doing all of this by reference. How can I add a TList to my TDictionary by value, instead of reference?

  // Create our dictionary of files and handlers
  for i := 0 to g_FileList.Count - 1 do
  begin
    g_HandlerName := AnsiMidStr(g_FileList[i], 2, Length(g_FileList[i]));
    g_HandlerName := AnsiMidStr(g_HandlerName, 1, Pos('"', g_HandlerName) - 1);

    if i = 0 then
      g_PreviousHandlerName := g_HandlerName;

    if AnsiCompareText(g_HandlerName, g_PreviousHandlerName) = 0 then
    begin
      g_FilePath := AnsiMidStr(g_FileList[i], Length(g_HandlerName) + 5, Length(g_FileList[i]));
      g_FilePath := AnsiMidStr(g_FilePath, 1, Length(g_FilePath) - 1);
      g_FilePaths.Add(g_FilePath);
    end
    else
    begin
      g_FileHandlers.Add(g_PreviousHandlerName, g_FilePaths);

      g_FilePaths.Clear;
      g_FilePath := AnsiMidStr(g_FileList[i], Length(g_HandlerName) + 5, Length(g_FileList[i]));
      g_FilePath := AnsiMidStr(g_FilePath, 1, Length(g_FilePath) - 1);
      g_FilePaths.Add(g_FilePath);
    end;

    if AnsiCompareText(g_HandlerName, g_PreviousHandlerName) <> 0 then
      g_PreviousHandlerName := g_HandlerName;

    if i = g_FileList.Count - 1 then
      g_FileHandlers.Add(g_HandlerName, g_FilePaths);
  end;
  g_FilePaths.Free;

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

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

发布评论

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

评论(1

甜妞爱困 2024-09-21 13:50:23

您拥有的 TList“值”一个引用,因此您按值添加它。 (通过引用添加意味着如果您更改了 g_FilePaths 的值,存储在字典中的值也会更改,但这种情况不会发生 - 这些值继续引用它们所指向的同一个 TList 对象。 TDictionary 不会像其他任何东西一样

进行对象的深层复制,您只需硬着头皮为您想要添加的每个项目创建一个新的 TList 对象即可。如果需要,可以使用全局变量 g_FilePaths ,但每次迭代都需要实例化一个新对象。

The TList "value" you have is a reference, so you are adding it by value. (Adding by reference would mean that if you changed the value of g_FilePaths, the values stored in the dictionary would change as well, but that doesn't happen — those values continue referring to the same TList object they started with.

TDictionary doesn't make deep copies of objects, just like nothing else does. You're just going to have to bite the bullet and create a new TList object for every item you wish to add. You can re-use the global variable g_FilePaths if you want, but you need to instantiate a new object each iteration.

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