Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my dictionary as follows

C#
Dictionary<int, MyDictionary> outerDictionary = new Dictionary<int, MyDictionary>();


This is how MyDictionary defined

C#
public struct MyValue
    {
        public List<int> lstPayPeriodID;
        public List<DateTime> lstPaymentDate;
    }


    public class MyDictionary : Dictionary<int, MyValue>
    {
        public void Add(int key, List<int> lstpayID, List<DateTime> lstPaymntDate)
        {
            MyValue v1 = new MyValue();
            v1.lstPayPeriodID = lstpayID;
            v1.lstPaymentDate = lstPaymntDate;
            this.Add(key, v1);
        }
    }


This is how i added keys and values to the dictionary
C#
for (int iempIdcnt = 0; iempIdcnt < EmpIDs.Length; iempIdcnt++)
        {
            if (!outerDictionary.TryGetValue(EmpIDs[iempIdcnt], out dictAddValues))
            {
                dictAddValues = new MyDictionary();
                outerDictionary.Add(EmpIDs[iempIdcnt], dictAddValues);
            }
        }


        for (int iDictCnt = 0; iDictCnt < iPayYear.Length; iDictCnt++)
        {
            if (!(dictAddValues.ContainsKey(iPayYear[iDictCnt])))
            {
                List<int> lstiPayperiodID = new List<int>();
                List<DateTime> lstiDateTime = new List<DateTime>();
                dictAddValues.Add(iPayYear[iDictCnt], lstiPayperiodID, lstiDateTime);
            }
            dictAddValues[iPayYear[iDictCnt]].lstPaymentDate.Add(dtPaymentDates[iDictCnt]);
            dictAddValues[iPayYear[iDictCnt]].lstPayPeriodID.Add(iPayPeriodID[iDictCnt]);
        }


Trying to loop through each dictionary

C#
foreach (int outerKey in outerDictionary.Keys)
        {
            MyDictionary outerValue = outerDictionary[outerKey]; // Here I am not getting the values for first time..
            foreach (int innerKey in outerValue.Keys)
            {
                -------------------
                ------------------- My Code
                ------------------- 
             }
         }


While iterating through first key I am unable to get values in outerValue, can any one tell where I went wrong..
Posted

1 solution

I think it may be a problem with your dictionary loading. I tried to set up a similar (but simpler) system for testing:

C#
private class MyDict : Dictionary<int, string>
    {
    public override string ToString()
        {
        StringBuilder sb = new StringBuilder();
        foreach( KeyValuePair<int, string> kvp in this)
            {
            sb.AppendFormat("MyDict {0}:{1}\n", kvp.Key, kvp.Value);
            }
        return sb.ToString();
        }
    }
private void button1_Click(object sender, EventArgs e)
    {
    Dictionary<int, MyDict> dict = new Dictionary<int, MyDict>();
    dict.Add(0, NewDict(0));
    dict.Add(1,NewDict(1));
    dict.Add(2, NewDict(2));
    foreach (int i in dict.Keys)
        {
        Console.WriteLine("{0} : {1}", i, dict[i]);
        }
    }
private MyDict NewDict(int i)
    {
    MyDict dict = new MyDict();
    dict.Add(10, i.ToString() + "=zero");
    dict.Add(1, i.ToString() + "=one");
    dict.Add(2, i.ToString() + "=two");
    return dict;
    }


When I press the button, I get what I would expect:
0 : MyDict 0:0=zero
MyDict 1:0=one
MyDict 2:0=two

1 : MyDict 0:1=zero
MyDict 1:1=one
MyDict 2:1=two

2 : MyDict 0:2=zero
MyDict 1:2=one
MyDict 2:2=two

Have you check the data loaded is what you expect it to be?

[edit]Bloody HTML encode swallowing tags! Grumble, grumble... - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
demouser743 24-Mar-12 5:32am    
yeah the data is not getting loaded as I expected.
demouser743 24-Mar-12 5:48am    
Probably I am going wrong at this statement

In EmpID's I will get 3 ID's like 101,102,102
for (int iempIdcnt = 0; iempIdcnt < EmpIDs.Length; iempIdcnt++)
{
if (!outerDictionary.TryGetValue(EmpIDs[iempIdcnt], out dictAddValues))
{
dictAddValues = new MyDictionary();
outerDictionary.Add(EmpIDs[iempIdcnt], dictAddValues);
}
}
As 102 already exists I am unable to add the dictAddValues for the 3rd loop. How can I add this
OriginalGriff 24-Mar-12 5:51am    
I suspect you will need to convert the 'if' to a loop, and add them individually if they aren't in there - that or change TryGetValue to only return unique ones - I can't tell from this distance!

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