2017-03-02 93 views
-4

我有这个错误,当我从字典中调用值,但我没有看到错误。 我的目标是从字典中获取具有值的索引。具有相同键的项目已添加 - 字典调用值c#

任何解决方案?

我的错误:

An item with the same key has already been added.

我dictionaryclass

public static class DictionaryParamiters 
{ 
    public static Dictionary<int, String> languagesDictionary = new Dictionary<int, String> 
    { 
     { 0, "ITA" }, 
     { 1, "ENG" }, 
     { 2, "FRA" }, 
     { 3, "SPA" }, 
     { 4, "DEU" } 
    }; 

    public static Dictionary<int, String> modelDictionary = new Dictionary<int, String> 
    { 
     { 0, "IMX BT" }, 
     { 1, "IMX B" }, 
     { 2, "MMX BT" }, 
     { 3, "MMX B" }, 
     { 4, "IMX BT (no logo)" }, 
     { 5, "IMX B (no logo)" } 
    }; 

    public static Dictionary<int, String> batteryDictionary = new Dictionary<int, String> 
    { 
     { 0, "GEL" }, 
     { 1, "WET" }, 
     { 2, "XFC" }, 
     { 3, "GEL1" }, 
     { 4, "WET1" } 
    }; 

    public static Dictionary<int, String> batteryMMGDictionary = new Dictionary<int, String> 
    { 
     { 0, "PB" }, 
     { 1, "GEL" }, 
     { 2, "XFC" }, 
     { 3, "PB1" }, 
     { 4, "GEL1" }, 
     { 5, "CUSTOM" } 
    }; 

    public static Dictionary<int, String> partialHurmeterdisplayDictionary = new Dictionary<int, String> 
    { 
     { 0, "KEY" }, 
     { 1, "TR" }, 
     { 2, "BR" }, 
     { 3, "VAC" } 
    }; 

    public static Dictionary<int, String> resetPartialHourmeterDictionary = new Dictionary<int, String> 
    { 
     { 0, "NO" }, 
     { 1, "KEY" }, 
     { 2, "TR" }, 
     { 3, "BR" }, 
     { 3, "VAC" }, 
     { 3, "ALL" } 
    }; 

    public static Dictionary<int, String> resetMainHourmeterDictionary = new Dictionary<int, String> 
    { 
     { 0, "NO" }, 
     { 1, "KEY" }, 
     { 2, "TR" }, 
     { 3, "BR" }, 
     { 3, "VAC" }, 
     { 3, "ALL" } 
    }; 

    public static Dictionary<int, String> serviceWarningCountDictionary = new Dictionary<int, String> 
    { 
     { 0, "KEY" }, 
     { 1, "TR" }, 
     { 2, "WORK" } 
    };   

    public static Dictionary<int, String> noyesDictionary = new Dictionary<int, String> 
    { 
     { 0, "NO" }, 
     { 1, "YES" } 
    }; 

    public static Dictionary<int, String> displayCntDictionary = new Dictionary<int, String> 
    { 
     { 0, "KEY" }, 
     { 1, "TRACTION" } 
    }; 

    public static Dictionary<int, String> zerooneDictionary = new Dictionary<int, String> 
    { 
     { 0, "0" }, 
     { 1, "1" } 
    }; 

    public static Dictionary<int, String> baseversionDictionary = new Dictionary<int, String> 
    { 
     { 0, "RULLO" }, 
     { 1, "DISCO" }, 
     { 2, "ORBITALE" } 
    }; 

    public static Dictionary<int, String> modelMMGOPLUSDictionary = new Dictionary<int, String> 
    { 
     { 0, "MMG" }, 
     { 1, "PLUS" } 
    }; 
} 

当我打电话

if (tmp.PMC_UM == "") 
      { 
       string valore = String.Empty; 
       //devo prendere la x ed estrarla dal dizionario per stampare le stringhe 
       #region VERIFICO LE STRINGHE E LE PRELEVO DAI DICTIONARY 
       if (tmp.PMC_Descrizione == "Language") 
       { 
        valore = DictionaryParamiters.languagesDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Model" && DataFile.instance.PMC_SUB_Tipo_Click == null) 
       { 
        valore = DictionaryParamiters.modelDictionary[(int)x]; 
       } 

       //if (tmp.PMC_Descrizione == "Model" && DataFile.instance.PMC_SUB_Tipo_Click != null) 
       //{ 
       // valore = DictionaryParamiters.modelMMGOPLUSDictionary[(int)x]; 
       //} 

       if (tmp.PMC_Descrizione == "Rst Cnthr"/* || tmp.PMC_Descrizione == "Rst Main Cnthr" || tmp.PMC_Descrizione == "Side brush" || tmp.PMC_Descrizione == "Daylight Enable" || tmp.PMC_Descrizione == "Daylight Enable" || tmp.PMC_Descrizione == "Worklight Enable " || tmp.PMC_Descrizione == "Dosing system" || tmp.PMC_Descrizione == "Recycle" || tmp.PMC_Descrizione == "Anticollision" || tmp.PMC_Descrizione == "Rear camera" || tmp.PMC_Descrizione == "Manual Op. Enable" || tmp.PMC_Descrizione == "Zone Op. Enable" || tmp.PMC_Descrizione == "Password Enable: User" || tmp.PMC_Descrizione == "Password Enable: PIN"*/) 
       { 
        valore = DictionaryParamiters.noyesDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Battery") 
       { 
        valore = DictionaryParamiters.batteryDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Battery Type") 
       { 
        valore = DictionaryParamiters.batteryMMGDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Display Cnt") 
       { 
        valore = DictionaryParamiters.displayCntDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Base version") 
       { 
        valore = DictionaryParamiters.baseversionDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Partial Hurmeter display") 
       { 
        valore = DictionaryParamiters.partialHurmeterdisplayDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Reset Partial Hourmeter") 
       { 
        valore = DictionaryParamiters.resetPartialHourmeterDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Reset Main Hourmeter") 
       { 
        valore = DictionaryParamiters.resetMainHourmeterDictionary[(int)x]; 
       } 

       if (tmp.PMC_Descrizione == "Service Warning Count") 
       { 
        valore = DictionaryParamiters.serviceWarningCountDictionary[(int)x]; 
       } 
       #endregion 

       par.ValoreSettatoOra = valore; 
      } 

i'dont找到重复的值... :(

+4

您的'resetPartialHourmeterDictionary'和'resetMainHourmeterDictionary'具有重复的键。 – Zroq

+0

您的第六和第七个字典有多个密钥“3” – pinkfloydx33

+0

但它们有两个不同的字典名称,但是 –

回答

1

你插入Key值3多次。在字典中,每个键必须是唯一的。

public static Dictionary<int, String> resetPartialHourmeterDictionary = new Dictionary<int, String> 
    { 
     { 0, "NO" }, 
     { 1, "KEY" }, 
     { 2, "TR" }, 
     { 3, "BR" }, 
     { 3, "VAC" }, 
     { 3, "ALL" } 
    }; 
+0

我也有错误 –

0

在你resetPartialHourmeterDictionaryresetMainHourmeterDictionary,你有相同的密钥3那里。

相关问题