Good question. My code works fine with a boolean, but not with a double. Here is my code:
Code:
// this boolean code works fine
string key = "n";
if (!Global.Contains (key))
Global[key] = true;
bool global_n = (bool) Global[key];
Console.WriteLine ("N is {0}", global_n);
// but this double code fails with the 'cast not valid' runtime error
string key = "n";
if (!Global.Contains (key))
Global[key] = 100000;
double global_n = (double) Global[key];
Console.WriteLine ("N is {0}", global_n);
// finally, this code works fine. I guess C# uses a different
// storage representation for 100000 if you don't tell it to store
// it as a 'double' bit pattern
string key = "n";
if (!Global.Contains (key))
Global[key] = (double) 100000;
double global_n = (double) Global[key];
Console.WriteLine ("N is {0}", global_n);
I tried using no cast on the store operation, and then 'long' or 'float' on the retrieve operation, to see if long or float might have been the default storage rep going in. But neither one worked, so I don't know what storage rep is used going in. Anyhow, this particular problem is now solved.
