2

I have a NameValueCollection with string keys and string values, like this:

fields["fieldname"] = "string value"

When I add a WATCH in VS20217 for "fields" it lets me expand a list of keys, but I don't seem to be able to get to the values unless I add an individual watch for each key.

I seem to recall doing this thing in the past but can't recall the method. Can anybody help?

Mike Fulton
  • 920
  • 11
  • 22
  • Possibly a duplicate: please take a look at this one https://stackoverflow.com/questions/6967173/get-all-values-of-a-namevaluecollection-to-a-string – Alexander Bell Oct 05 '17 at 01:06
  • Thanks for your answer, but i'm afraid that's not a duplicate. The other question is about doing it in your C# code, whereas I am trying to set up a watch in the debugger. Two very different scenarios. – Mike Fulton Oct 05 '17 at 01:13

1 Answers1

1

The low-friction approach would be to create an extension method first:

public static string DebugView(this NameValueCollection nvc)
{
    return string.Join("\r\n", nvc.AllKeys.Select(key => $"{key}: {nvc[key]}"));
}

Then put nvc.DebugView() as your watch variable, or capture that string to a variable and put a watch on that instead of the NameValueCollection itself.

Paul Smith
  • 3,104
  • 1
  • 32
  • 45
  • 1
    That's something I'll consider if I can't figure out a better way. Thing is, I remember doing this sort of thing in the past and it didn't take anything like writing extension methods. – Mike Fulton Oct 05 '17 at 16:22
  • You could also try adding a custom debug visualizer to your Visual Studio. Here's an example of code for a NameValueCollection visualizer: https://searchcode.com/codesearch/view/10031420/. Here's general info from Microsoft on creating, installing and using custom visualizers: https://msdn.microsoft.com/en-us/library/zayyhzts.aspx – Paul Smith Oct 05 '17 at 16:53
  • On the other hand, I just implemented my original suggestion to test it out, and it took me all of 45 seconds and 8 lines of code (including the extension class declaration and all the open/close curly braces). :-) – Paul Smith Oct 05 '17 at 17:02