gauri sharma

gauri sharma

  • NA
  • 25
  • 2.6k

Nested Dictionary looping challenge need help!

Dec 9 2014 8:45 PM
when i add some keys and values to nested dictionary it (values ) turns up on all key values here is a sample code may be i am making some mistake??
 

static void Main(string[] args)
{
basictest();
}

//Nested dictionary problem
public static Dictionary<int, Dictionary<DateTime, String>> nestedict5 = new Dictionary<int, Dictionary<DateTime, string>>();

public static void basictest() {

int mk = 7001, nk = 7000; // just select 2 int values one odd(7001) and even(7000) for outer dictionary
DateTime ddn = DateTime.Now; // loop working with Datetime.Now for// inner dictionay
Dictionary<DateTime, string> cd = new Dictionary<DateTime, string>();
nestedict5.Add(mk, cd); nestedict5.Add(nk, cd); //adding a new odd and //even dictionary to outer dictionary
for (int i = 0; i < 12; i++)
{
ddn = ddn.AddMinutes(1); // adding a minute to time



if (ddn.Minute % 2 != 0) // if minute is odd
{
if (!(nestedict5[mk]).ContainsKey(ddn)) //check if outer dictionary odd key //contains this odd minute value
{

(nestedict5[mk]).Add(ddn, "odd"); // add odd string to inner with key of datetime,Minute odd
printvalues(mk); // check results of odd keys in debug windows
}
}
else if (ddn.Minute % 2 == 0) // if ninute is even
{
if (!(nestedict5[nk]).ContainsKey(ddn)) //check if even outer dictionay key //contains this even minute value
{

(nestedict5[nk]).Add(ddn, "even"); // add even string to inner with //key of Minute even
printvalues(nk); // check results of even keys in debug windows
}
}
}
}

// loop to print odd and even key values in debug window
public static void printvalues(int k)
{
var cc = (nestedict5[k]).Values.ToArray();
var ck = (nestedict5[k]).Keys.ToArray();
for (int i = 0; i < cc.Count(); i++)
{
DateTime dt = ck[i];
String dcandle = cc[i];
Debug.WriteLine(string.Format("pids1:{0},Count{1}, datetime:{2}: ticks:{3}", k * 100 + i, cc.Count(), ck[i], cc[i]));

}

}
Question is at the end of the loop i shall get 6 members in both odd and even minute
whereas due to some error i am getting 12 members in all/both odd and even key outer dictionary help

Here is output in debug windows
pids1:700100,Count1, datetime:12/10/2014 6:39:07 AM: ticks:odd ok

even key results:
pids1:700000,Count2, datetime:12/10/2014 6:39:07 AM: ticks:odd not ok
pids1:700001,Count2, datetime:12/10/2014 6:40:07 AM: ticks:even ok

odd key results:
pids1:700100,Count3, datetime:12/10/2014 6:39:07 AM: ticks:odd ok
pids1:700101,Count3, datetime:12/10/2014 6:40:07 AM: ticks:even not ok
pids1:700102,Count3, datetime:12/10/2014 6:41:07 AM: ticks:odd ok


The problem continue's so on till all loops as odd and even keys appear in both key values!?? which is not desired and only odd key should have odd values and even key even values



pids1:700000,Count4, datetime:12/10/2014 6:39:07 AM: ticks:odd
pids1:700001,Count4, datetime:12/10/2014 6:40:07 AM: ticks:even
pids1:700002,Count4, datetime:12/10/2014 6:41:07 AM: ticks:odd
pids1:700003,Count4, datetime:12/10/2014 6:42:07 AM: ticks:even



Answers (1)