8

I can't get TryGetValue to work for some reason.

Dictionary<String,String> testdict = new Dictionary<String,String>();
String teststr = "test";
if(testdict.TryGetValue(teststr,out value))
{
    //Ladida
}

Error received:

The best overloaded method match for 'System.Collections.Generic.Dictionary<string,string>.TryGetValue(string, out string)' has some invalid arguments

Can anyone tell me what's wrong with my code?

1
  • 3
    Looks like value is not a variable of type string. We can't see it. Mar 31, 2012 at 16:04

3 Answers 3

9

Add this line after creating the dictionary:

String value = "";
0
3

It looks like the problem is that value isn't properly typed to string. This is the only reason that you would get that particular error. You need to change the type of value to string or declare a new variable of type string to use in TryGetValue

0

Maybe something like this:

Dictionary<String,String> testdict = new Dictionary<String,String>();
string theValueYouAreTryingFor = "test";
string theValueYourGetting;
if(testdict.TryGetValue(theValueYouAreTryingFor,out theValueYourGetting))
{
    //If the value is in the Dictionary
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.