Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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?
C#
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)){

......
}
}
Posted
Updated 27-Aug-15 23:56pm
v2

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"[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Aug-15 10:43am    
5ed.
—SA
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:
C#
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:
C#
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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900