Dictionary.ContainsKey永远找不到密钥(使用集合初始化程序) [英] Dictionary.ContainsKey never finds a key (using a collection Initializer)

查看:135
本文介绍了Dictionary.ContainsKey永远找不到密钥(使用集合初始化程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有intilizer集合的字典。我想将它用作我的密钥,但是当我进行检查时,它从未在我的dic MAP_ALARMS中找到它们。为什么?我的代码有什么问题?我还尝试将AlarmMapped对象首先存储在

 List< AlamrMapped>中。 mapList 

然后用作我的字典mapList [0],mapList [1]的键。但是,它仍然没有找到任何键匹配。问题是什么?

< pre lang =cs> public class AlarmMapped
{
public string SubsystemId { get ; set ; }
public string 代码{ get ; set ; }
public string SystemId { get ; set ; }
public AlarmMapped( string systemId, string subsystemId, string subsystemCode)
{
SystemId = systemId;
SubsystemId = subsystemId;
Code = subsystemCode;
}
}

public 字典< AlarmMapped,字符串> MAP_ALARMS = new 字典< AlarmMapped,字符串>()
{
{ new AlarmMapped( SYSTEM_STERRING < span class =code-string> SUBSYSTEM_STERRING_POWERUNIT
004),< span class =code-string>
Phase Fail},
{ new AlarmMapped( SYSTEM_STERRING SUBSYSTEM_STERRING_POWERUNIT 005 ), 液压油液位},

};

public void myFunction(){

CheckMap( new AlarmMapped( SYSTEM_STERRING SUBSYSTEM_STERRING_POWERUNIT 005);
}

public void CheckMap(AlarmMapped m){

if (MAP_ALARMS.ContainsKey(m)){

......
}
}

解决方案

您正在比较引用对象(对于不同的对象,它们不能相等,即使它们包含相同的数据)。

见这里:比较用作词典中键的对象 [ ^ ]。


嗯。

AlarmMapped是一个类,这意味着你的Dictionary包含对类实例的引用和对a的引用。字符串。

因此,对字典键进行检查以查找matych将比较类引用,而不是类内容。

因此,如果您的字典包含以下元素:

  public 字典< AlarmMapped,string> MAP_ALARMS =  new 字典< AlarmMapped,字符串>()
{
{ new AlarmMapped( SYSTEM_STERRING < span class =code-string> SUBSYSTEM_STERRING_POWERUNIT, 004),< span class =code-string> Phase Fail},
{ new AlarmMapped( SYSTEM_STERRING SUBSYSTEM_STERRING_POWERUNIT 005 ), 液压油液位},

};

然后你针对你班级的新实例进行检查:

 CheckMap( new  AlarmMapped(  SYSTEM_STERRING  SUBSYSTEM_STERRING_POWERUNIT  005); 

您将检查是否有新实例引用存在于字典中 - 根据定义它不会,因为它是 new 实例,即使它包含相同的数据。



这就像把你的手机放在你的办公桌抽屉里,然后期望在你的办公桌抽屉里找到的手机中找到你的联系人和信息!


I have a dictionary with a intilizer collection. I want to use that as my key but it never founds them in my dic MAP_ALARMS when I do the check. Why? what is wrong in my code? I also tried storing first the AlarmMapped objcts in a

List<AlamrMapped> mapList

and then use as keys of my dictionary mapList[0],mapList[1].. but still , it never finds any key matching.Whats the problem?

public class AlarmMapped
    {
        public string SubsystemId { get; set; }
        public string Code { get; set; }
        public string SystemId { get; set; }
        public AlarmMapped(string systemId, string subsystemId, string subsystemCode)
        {
            SystemId = systemId;
            SubsystemId = subsystemId;
            Code = subsystemCode;
        }
}

public Dictionary<AlarmMapped, string> MAP_ALARMS = new Dictionary<AlarmMapped, string>()
       {
        {  new AlarmMapped ("SYSTEM_STERRING","SUBSYSTEM_STERRING_POWERUNIT","004"),"Phase Fail"},
        {  new AlarmMapped ("SYSTEM_STERRING","SUBSYSTEM_STERRING_POWERUNIT","005"),"Hydraulic fluid level"},

       };

public void myFunction(){

CheckMap(new AlarmMapped("SYSTEM_STERRING","SUBSYSTEM_STERRING_POWERUNIT","005");
}

public void CheckMap(AlarmMapped m){

if(MAP_ALARMS.ContainsKey(m)){

......
}
}

解决方案

You are comparing references to objects (they cannot be equal for differenct objects, even if they contain same data).
See here: "Comparing object used as Key in Dictionary"[^].


Um.
AlarmMapped is a class, which means that your Dictionary consists of a reference to a class instance, and a reference to a string.
So any checking against the dictionary keys to find a matych will compare class references, not class content.
So if your dictionary contains these elements:

public Dictionary<AlarmMapped, string> MAP_ALARMS = new Dictionary<AlarmMapped, string>()
       {
        {  new AlarmMapped ("SYSTEM_STERRING","SUBSYSTEM_STERRING_POWERUNIT","004"),"Phase Fail"},
        {  new AlarmMapped ("SYSTEM_STERRING","SUBSYSTEM_STERRING_POWERUNIT","005"),"Hydraulic fluid level"},

       };

And you check it against a new instance of your class:

CheckMap(new AlarmMapped("SYSTEM_STERRING","SUBSYSTEM_STERRING_POWERUNIT","005");

you will be checking to see if the new instance reference exists in the dictionary - which by definition it won't because it is a new instance, even if it contains the same data.

It's like putting your mobile phone in your desk drawer, and then expecting to find your contacts and messages in a phone you found in my desk drawer!


这篇关于Dictionary.ContainsKey永远找不到密钥(使用集合初始化程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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