C# - 如何查看字典中的值 [英] C# - How to see the values in a dictionary

查看:135
本文介绍了C# - 如何查看字典中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以在字典中保存值,然后再次查找它以使用特定键返回值。我为字典硬编码添加了参数,但VS无法在字典中找到它。错误是VS无法在字典中找到
请求的密钥。

I have a program that saves values in a dictionary and then looks it up again to return the value with the specific key. I added a parameter for the dictionary hard-coded, but VS cannot find it in the dictionary. The error was that VS could not find the requested key in the dictionary.

添加参数的代码:

public override IDictionary<DParam, Dictionary<int, string>> getDefaultParams()
        {
            IDictionary<DParam, Dictionary<int, string>> defaultparams = new Dictionary<DParam, Dictionary<int, string>>();

            defaultparams.Add(new DParam { pluginid = FPlugin.id, type = DParam.EType.value.ToString(), naam = ExampleParams.reset.ToString(), omschrijving = "Reset counters in de PLC", datatype = DParam.EDatatype.@bool.ToString(), adres = "5", ingeschakeld = DParam.EIngeschakeld.niet.ToString(), nietverwijderbaar = true, volgorde = 1, eenheid = "" }, new Dictionary<int, string>());
            defaultparams.Add(new DParam { pluginid = FPlugin.id, type = DParam.EType.value.ToString(), naam = ExampleParams.aantalok.ToString(), omschrijving = "Aantal dozen OK", datatype = DParam.EDatatype.@integer.ToString(), adres = "74", ingeschakeld = DParam.EIngeschakeld.altijd.ToString(), nietverwijderbaar = true, volgorde = 2, eenheid = "stk.", eenheidTellen = "stk." }, new Dictionary<int, string>());
            
            return defaultparams;
        }

这种情况下的DParam用于数据库(语言是荷兰语,所以如果你需要翻译,请告诉我):

DParam in this case is used for the database (the language is Dutch, so if you need translations please tell me):

//mogelijke waardes van de eigenschap type.
        public enum EType { value, variable };
        //mogelijke waardes van de eigenschap datatype. deze word ook gebruikt in de tabel Config.
        public enum EDatatype { @string, integer, integer32, @bool, boolDropdown, @double, double1, double2, double3, double4, double5, double6, double7, double8, dropdown };
        //mogelijke waardes van de eigenschap ingeschakeld
        // ReSharper disable UnusedMember.Global
        public enum EIngeschakeld { altijd, niet, tellen, wegen };
        // ReSharper restore UnusedMember.Global

        [Indexed]
        public virtual int pluginid { get; set; }

        public virtual string type { get; set; }
        public virtual string datatype { get; set; }
        public virtual string eenheid { get; set; }
        public virtual string eenheidTellen { get; set; }
        public virtual string category { get; set; }
        public virtual string adres { get; set; }
        public virtual string naam { get; set; }
        public virtual int volgorde { get; set; }
        public virtual bool zichtbaar { get; set; }
        public virtual bool shortlist { get; set; }
        public virtual string standaardwaarde { get; set; }
        public virtual float? ondergrens { get; set; }
        public virtual float? bovengrens { get; set; }
        public virtual string omschrijving { get; set; }
        public virtual string ingeschakeld { get; set; }
        public virtual int verwijzing { get; set; }
        public virtual bool littleendian { get; set; }
        public virtual bool nietverwijderbaar { get; set; }

        //public virtual DPlugin plugin { get; set; } //TODO: nodig voor eager loading.

        public static DParam getStringParam(int APluginid, string ANaam, int AVolgorde, string AAdres = "")
        {
            return new DParam()
            {
                pluginid = APluginid,
                type = EType.value.ToString(),
                datatype = EDatatype.@string.ToString(),
                eenheid = "",
                eenheidTellen = "",
                category = "",
                adres = AAdres,
                naam = ANaam,
                volgorde = AVolgorde,
                zichtbaar = true,
                shortlist = false,
                standaardwaarde = "",
                ondergrens = 0,
                bovengrens = null,
                omschrijving = "",
                ingeschakeld = EIngeschakeld.altijd.ToString(),
                verwijzing = -1,
                littleendian = false,
                nietverwijderbaar = false
            };
        }        
    }

当我想查看字典中的内容时,我使用:

When I want to look something in the dictionary, I use:

if (paramList.TryGetValue(ExampleParams.aantalok.ToString(), out tempvalue))
                {
                    aantalok_previous = tempvalue.ParseAsInt(0);

                }

但是,我得到的结果是tempvalue是Null。 (使用breakmode)。我检查了休息模式,它显示它搜索了"aantalok",但不知何故它仍然从字典中返回Null,因为VS显示它无法找到它作为
a键。

However, I get the answer that tempvalue is Null. (using a breakmode). I checked the break mode and it showed that it searched for "aantalok", but somehow it still returned Null from the dictionary, because VS showed that it could not find it as a key.

所以我的问题是:

1)我在这里做错了吗?如果是这样,我该如何做?

1) Did I do something wrong here? If so, how do I do it right?

2)我如何自己检查字典中的值?

2) How can I check the values in the dictionary myself?

如果您需要额外的信息,请告诉我。

If you need extra info, please tell me.

提前致谢,Ganesh

Thanks in advance, Ganesh

推荐答案

我对你的例子感到有点困惑。

I am a bit confused by your examples.

你的第一个getDefaultParams代码创建了一个字典字典。第一个字典使用DParam类作为键,其值是带有int键的字典。

Your first code for getDefaultParams creates a dictionary of dictionaries. The first dictionary uses the DParam class as a key, with values that are dictionaries with an int key.

然而,您在字典中查找的示例使用字符串值作为查找( ExampleParams.aantalok.ToString())。我不希望这甚至编译,所以我必须遗漏一些东西。

And yet your example of looking in the dictionary uses a string value as a look up (ExampleParams.aantalok.ToString()). I would not expect that to even compile, so I must be missing something.

要记住的一件事是,如果你使用引用变量(像DParams这样的类)作为字典的关键,然后字典必须有一些方法来比较这个类的不同实例,以确定它们是否相等。

One thing to bear in mind though is if you use a reference variable (a class such as DParams) as a key to a dictionary, then the dictionary must have some way of comparing separate instances of this class to know if they are equal or not.

默认情况下,两个这样的类将不等于彼此,即使班级的所有属性都相同!

By default, two such classes will NOT be equal to each other, even if all the properties of the class are the same!

例如

Dictionary<DParam, string> testDict = new Dictionary<DParam, string>();

// Add value
DParam parm = new DParam() {eenheid = "BLAH"};

testDict.Add(parm, "Some value");

// Look up value

DParam parm2 = new DParam() {eenheid = "BLAH"};

string s = testDict[parm2];  // << WILL FAIL!

这会失败,因为即使parm和parm2包含具有完全相同属性值的DParam,它们也是两个独立的实例就字典而言,类和两个不相等的东西。它只有在
完全相同的DParam实例时才有效,即parm变量  用于添加和查找值。

This fails because even though parm and parm2 contain a DParam with exactly the same property values, they are two separate instances of the class and, as far as the dictionary is concerned, two separate unequal things. It would only work if the exact same instance of DParam, i.e. the parm variable was used for both adding and looking up the value.

如果您希望两个具有部分或全部相同属性值的DParams被视为"相同",那么您必须实现IEqualityComparer接口,或者您可以按照
这个例子

If you want two DParams with the some or all of the same property values to be considered 'identical' then you must implement the IEqualityComparer interface, or you can pass an EqualityComparer into the dictionary constructor as per this example


这篇关于C# - 如何查看字典中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆