-3

i've this error when i call the value from dictionary, but i don't see the error. my target is get index with value from dictionary.

any solution ?

my error:

An item with the same key has already been added.

my 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" }
    };
}

when i call

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 find duplicate value... :(

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
  • 4
    Your `resetPartialHourmeterDictionary` and `resetMainHourmeterDictionary` have duplicated keys. – Zroq Mar 02 '17 at 14:44
  • Your sixth and seventh Dictionaries have the key "3" multiple times – pinkfloydx33 Mar 02 '17 at 14:45
  • but they have two different names of the dictionary, however – Mr. Developer Mar 02 '17 at 14:45
  • Use a `Dictionary>` instead. Then you can have such a dictionary: `new Dictionary> { { 0, new List{"NO"} }, { 1, new List{"KEY"} }, { 2, new List{"TR"} }, { 3, new List{"BR","VAC","ALL"} } };` – Tim Schmelter Mar 02 '17 at 14:47
  • @Mr.Developer Different variable names don't matter. What matters is that you have a single dictionary and you have the same key specified multiple times. That just won't work. – mason Mar 02 '17 at 14:47
  • `but they have two different names of the dictionary, however ` - That's not how dictionaries work, keys must be unique, values don't matter. – Equalsk Mar 02 '17 at 14:47

2 Answers2

1

you are inserting the Key value 3 multiple times. In a Dictionary each Key must be unique.

public static Dictionary<int, String> resetPartialHourmeterDictionary = new Dictionary<int, String>
    {
        { 0, "NO" },
        { 1, "KEY" },
        { 2, "TR" },
        { 3, "BR" },
        { 3, "VAC" },
        { 3, "ALL" }
    };
Jason
  • 86,222
  • 15
  • 131
  • 146
0

In your resetPartialHourmeterDictionary and resetMainHourmeterDictionary, you have same key 3 there.

Liu
  • 970
  • 7
  • 19