2

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.

14
  • What if you try using a string instead? string RoadmapGroupValue = "0"; Commented Sep 22, 2017 at 16:51
  • @ShakSmith I tried that, the TryGetValue still evaluates to false and never runs the code it should if it finds the key. Commented Sep 22, 2017 at 16:55
  • How is the dictionary declared? What is the type of it's values? Commented Sep 22, 2017 at 16:59
  • Can you try something like workItem.Fields["CustomScrum.RoadmapGroup"] and see if you can retrieve your value? Commented Sep 22, 2017 at 17:02
  • @Sach Ok, if I modify my code so the dictionary only includes items where the "CustomScrum.RoadmapGroup" key exists and then replace the TryGetValue block with feature.RoadmapGroup = (int)workItem.Fields["CustomScrum.RoadmapGroup"];, I get a SystemInvalidCastException error. I'm casting to an integer because feature.RoadmapGroup is an integer. Commented Sep 22, 2017 at 17:13

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And here is an explanation of the differences between the two castings. So looks like you're indeed adding a non-numeric variable as the value 1, and thus your (int) cast goes wrong but Convert.ToInt32() doesn't. You'd either want to fix how you add the values to the Dictionary or better yet use the proper casting function.
Yep, you nailed it. I declared my RoadmapGroupValue variable as an Object instead of an Integer, then in the TryGetValue block I cast the output value to Int32 as you suggested and it works. I still think I could have saved myself hours if Visual Studio had thrown a type conversion error rather than just seeming to work with the TryGetValue statement simply evaluating to FALSE, seems like a small bug to me. Anyway, thanks for your help!
0

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.

2 Comments

The value is not a string, it's an integer. Is that what you mean? The key name is a string, as far as I can tell - can it be anything else?
What I mean, it's that the key for which you trying get value, can contains some non printable characters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.