1

I have a dictionary am getting thee value from the dictionary dynamically, but some times its gives exception . Can someone kindly help on this

How can i make a check the value of test[typeof(T)].Key.ColumnName, before doing some action in dictionary.

If i use !string.isnullorEmpty , there itself its throwing error. code

 int ID=123;
 private Dictionary<Type, DataTableAttribute> test
 parameters.AddInt32Parameter(test[typeof(T)].Key.ColumnName, ID);

-- thanks

poc
  • 183
  • 2
  • 3
  • 14
  • 1
    What is your question, is it "how do I try to retrieve the value from the dictionary and handle gracefully if the key does not exist" or "why doesn't the key exist in the dictionary" or "what does this exception mean anyway"? – Lasse V. Karlsen Feb 15 '16 at 15:22
  • Furthermore: the post you´ve provided does not compile at all thus cannot throw any exception. – MakePeaceGreatAgain Feb 15 '16 at 15:23

1 Answers1

2

You can use ContainsKey to avoid the exception

int ID=123;
Dictionary<Type, DataTableAttribute> test

// fill test-dictionary

if (test.ContainsKey(typeof(T)) 
{
    parameters.AddInt32Parameter(test[typeof(T)].Key.ColumnName, ID);
}

Alternativly use TryGetValue:

DataTableAttribute attr;
if (test.TryGetValue(typeof(T), out attr) 
{
    // ...
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111