0

I have two objects that I initialize like this:

var series = new Dictionary<string,Dictionary<string,int>>()
{
   {"0", new Dictionary<string, int>() },
   {"1", new Dictionary<string, int>() },
   {"2", new Dictionary<string, int>() }
}

var periodValues = new Dictionary<string,int>();
{
   {"Jan", 0 },
   {"Feb", 0 },
   {"Mar", 0 }
}

Then I have another object with filled values:

 var target = new Dictionary<string,Dictionary<string,int>>()
 {
       { "0", 
         new Dictionary<string, int>()
         {
            {"Jan", 12 },
            {"Mar", 22 }
         } 
       },
       { "1", 
         new Dictionary<string, int>()
         {
            {"Mar", 37 }
         } 
       },
       { "2", 
         new Dictionary<string, int>()
         {
            {"Jan", 4 },
            {"Feb", 48 },
            {"Mar", 22 }
         } 
       }
 }

series and target have always the same Key, while target[key].Keys (with key of type string) for any key can be a subset (at the most the same) of periodValues.Keys.

Now I want to fill series.Values according to the keys in periodValues.Keys but with the values of target[key].Value. Therefore:

foreach (var numberValue in target.Keys)
{
    foreach (var period in target[numberValue].Keys)
    {
        periodValues[period] = target[numberValue][period];
    }
    series[numberValue] = periodValues;
}

But I fall in the outer variable trap...that means all series[key] for any series.Keys are equal to the last periodValues. I tried many solutions to escape the outer variable trap according to this article with no luck. Anybody knows a solution or maybe a better approach?

CiccioMiami
  • 8,028
  • 32
  • 90
  • 151

1 Answers1

0

If anybody interested, I solved the problem by declaring and initializing periodValues inside the first foreach:

foreach (var numberValue in target.Keys)
{
    var periodValues = new Dictionary<string,int>();
    {
       {"Jan", 0 },
       {"Feb", 0 },
       {"Mar", 0 }
    }
    foreach (var period in target[numberValue].Keys)
    {
        periodValues[period] = target[numberValue][period];
    }
    series[numberValue] = periodValues;
}
CiccioMiami
  • 8,028
  • 32
  • 90
  • 151