简体   繁体   English

有什么方法可以使字典存储价值而不是指向价值?

[英]Any way of getting the dictionary to store value rather than pointing to value?

So I have a dictionary: 所以我有一本字典:

var tableVars = new Dictionary<string, TableDLL.tableObject>();

And the tableObject class: 和tableObject类:

public class tableObject
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public string Value { get; set; }
        public string Default { get; set; }
    }

And I go through this loop: 我经历了这个循环:

var obj = new TableDLL.tableObject();
        foreach (XmlNode nodes in root.ChildNodes)
        {
            obj.Value = nodes.InnerText;
            tableVars.Add(nodes.Name, obj);
        }

When the loop is complete, I have 65 different Dictionary entries, but they are all pointing to the LAST obj that is made in the loop. 循环完成后,我有65个不同的Dictionary条目,但是它们都指向循环中创建的LAST obj What would be the best way to get the Dictionary to store the value, rather than simply pointing to it (and therefore changing with changes to obj )? 什么是使Dictionary存储值而不是简单地指向它(并随obj改变而变化)的最佳方法? Do I have to make a new TableDLL.tableObject for each Dictionary entry? 我是否必须为每个Dictionary条目创建一个新的TableDLL.tableObject

Move the creation of obj into the loop: obj的创建移入循环:

    foreach (XmlNode nodes in root.ChildNodes)
    {
        var obj = new TableDLL.tableObject();
        obj.Value = nodes.InnerText;
        tableVars.Add(nodes.Name, obj);
    }

Otherwise you are adding the same object multiple times. 否则,您将多次添加同一对象。

You're only creating a single object and continually updating Value with the last node. 您仅创建一个对象,并用最后一个节点不断更新Value With the correction, you're creating N objects with the appropriate Value . 通过更正,您将创建具有适当Value N个对象。

This: 这个:

    var obj = new TableDLL.tableObject();
    foreach (XmlNode nodes in root.ChildNodes)
    {
        obj.Value = nodes.InnerText;
        tableVars.Add(nodes.Name, obj);
    }

should be this: 应该是这样的:

    foreach (XmlNode nodes in root.ChildNodes)
    {
        var obj = new TableDLL.tableObject();
        obj.Value = nodes.InnerText;
        tableVars.Add(nodes.Name, obj);
    }

You just have to create it in the loop: 您只需要在循环中创建它:

foreach (XmlNode nodes in root.ChildNodes)
{
    var obj = new TableDLL.tableObject();
    obj.Value = nodes.InnerText;
    tableVars.Add(nodes.Name, obj);
}

Otherwise you're always adding the same object. 否则,您将始终添加相同的对象。

You've only created 1 object, so there is no possibility for the list to have 65 different objects. 您仅创建了1个对象,因此该列表不可能包含65个不同的对象。

Basically, what you did is: 基本上,您所做的是:

  1. Take a box. 拿一个盒子。
  2. Put something in the box. 在盒子里放些东西。
  3. Now replace that thing with another thing. 现在用另一件事代替那件事。

You haven't created a new container, you just changed the existing container's contents. 您尚未创建新容器,只是更改了现有容器的内容。

Do it like this: 像这样做:

    foreach (XmlNode nodes in root.ChildNodes)
    {
        var obj = new TableDLL.tableObject();
        obj.Value = nodes.InnerText;
        tableVars.Add(nodes.Name, obj);
    }

The difference being: 区别在于:

  1. Take a box. 拿一个盒子。
  2. Put something in the box. 在盒子里放些东西。
  3. Now take a new box. 现在拿一个新盒子。
  4. Put the other thing in the new box 将其他东西放在新盒子里
  5. etc... 等等...

If you want 65 tableObject objects, you will need to call new tableObject(); 如果需要65个tableObject对象,则需要调用new tableObject(); 65 times. 65次 Else you're just reusing the original object. 否则,您只是在重用原始对象。

Feel free to ask for more information if you need it. 如果需要,请随时询问更多信息。

Create a new copy of your tableObject each time round the loop. 每次围绕循环创建一个tableObject的新副本。

    foreach (XmlNode nodes in root.ChildNodes)
    {
        var obj = new TableDLL.tableObject();
        obj.Value = nodes.InnerText;
        tableVars.Add(nodes.Name, obj);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM