TryGetValue Evaluating to False Even When Key is Present

2.6k views Asked by At

This question is basically the same as this one, although the answer to that person's problem turned out to be a simple trailing space.

My issue is that I'm retrieving data from a web API as dictionary and then trying get the values out of it. I'm using TryGetValue because not every item in the dictionary will necessarily contain every key. For some reason, whilst I can get the value of one key with no problems at all when it's present, for another key TryGetValue always evaluates to false and therefore doesn't return the value, even though I can see in debug that the key is present.

So, this block always retrieves the value of the "System.Description" key if it's present:

string descriptionValue = "";
if (workItem.Fields.TryGetValue("System.Description", out descriptionValue))
{
    feature.Description = descriptionValue;
}

However, this almost identical block NEVER retrieves the value of the "CustomScrum.RoadmapGroup" key:

int RoadmapGroupValue = 0;
if (workItem.Fields.TryGetValue("CustomScrum.RoadmapGroup", out RoadmapGroupValue))
{
    feature.RoadmapGroup = RoadmapGroupValue;
}

As you can see in this screenshot, the dictionary DOES contain a key with a name exactly matching my TryGetValue statement: enter image description here

If I put a breakpoint on the code which should be run if the TryGetValue statement evaluates to true (feature.Description = descriptionValue;) it never gets hit.

The feature.RoadmapGroup variable gets set to 0 for every item in the dictionary.

I've been staring at this for the last two hours at least and I can't see what I'm doing wrong.

2

There are 2 answers

2
Sach On BEST ANSWER

Here's a scenario where your cast goes wrong.

private void foo()
{
    Dictionary<string, object> dict = new Dictionary<string, object>();
    object obj = new object();
    obj = "1";
    dict.Add("CustomScrum.RoadmapGroup", obj);
    object val;
    var result = dict.TryGetValue("CustomScrum.RoadmapGroup", out val);
    int value = (int)val;
}

TryGetValue() returns true, but the last line (the cast), throws System.InvalidCastException: 'Specified cast is not valid.', although if you use a breakpoint to see the dictionary content it looks like you have something that can be converted to an int. See below:

enter image description here

So I believe that when you add the value to the dictionary, you're not really adding an int but something that looks like an int.

EDIT

I just replaced int value = (int)val; with int value = Convert.ToInt32(val); which converts the value just fine. So you might want to try to use that and see if that works as well.

2
Enumix On

Are you sure that this "CustomScrum.RoadmapGroup" key is a string? If yes, then make sure that it doesn't contain any special unreadable character. You can just copy this value while debugging, put it in Watch window and check length/bytes representation, then do the same for hand-written string with the same content.