dr. evil
dr. evil

Reputation: 27265

Visualising lists in debug (or printing to immediate windows)

When I try to see the internal list of Dictionary item I hate to expand every single node one by one. I'm looking for an easier way to do this.

For example:

I've got a Dictionary object Dictionary(Of AnotherObject, Integer) and I want see a property of AnotherObject as a list during the debug.

Normally I'd use this:

For Each item As DictionaryEntry(Of AnotherObject, Integer) in myDict
          Debug.Writeline(item.Name)
Next

But immediate window doesn't support loops.

Is there any practical way to do this in immediate window or debug visualizers?

Upvotes: 5

Views: 4919

Answers (3)

unbob
unbob

Reputation: 381

(ancient question, but. . .) I use LINQ for this quite a bit. Depending on what's present in the Immediate Window's context, you can either use the static members of System.Linq.Enumerable with extension method syntax or as ordinary static methods.

I'm going to use C# syntax 'cuz I have no idea what the VB syntax for this is (perhaps some VB wizard will come along and fix this).

I just performed the experiment of starting a small console project with Debug>Step Into New Instance. I found I had to load System.Core to get things going

System.Reflection.Assembly.Load("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
args.Select(arg => $"{arg}").ToArray()
System.Linq.Enumerable.Select(args, arg => $"{arg}").ToArray()

(Obviously arg is a string here and doesn't need to be converted to a string - I'm just using $"{arg}" to show a concise way of formatting something as a string)

(unless I'm totally misunderstanding the OP's intent) I think the equivalent for the OP would be something like

myDict.Keys.Select(key => $"{key.PropertyOfKey}").ToArray()

Additionally, you get the power of LINQ to manipulate what you display - Where, OrderBy, Skip, etc.

Upvotes: 0

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

While you can't use loops in the immediate window, it does allow you to declare new variables, so you can create new lists etc. which can then be displayed in the watch window.

Upvotes: 2

Related Questions