4

I have a couple of massive data structures that are causing problems in my VB.NET application. After an exception is thrown and the application pauses, I'd like to run some code like:

For Each o As MyClass In myObjects
  If o.property = "value" Then debug.print(o.id)
Next

to diagnose the problem.

The problem is that the immediate window won't let me execute loops, and the myObjects collection contains far too many objects for me to find the offending one I want manually.

How can I find this object while the debugger is paused? Is this, or something similar, possible in the .NET IDE?

Flash
  • 15,945
  • 13
  • 70
  • 98

2 Answers2

6

You should be able to use the Immediate mode window in the IDE to execute commands like that, but the data has to be available within the current scope of the debugger.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • You can't run loops in the immediate window. – Omer Raviv Apr 30 '11 at 08:23
  • True, but you could use .ForEach and call an action method to print out each item. – Erik Funkenbusch Apr 30 '11 at 10:30
  • Is this possible with myObjects a Dictionary? – Flash Apr 30 '11 at 14:07
  • Yep, that's true. @Andrew: I believe the ForEach method mentioned here is the one that appears in the 2nd answer to this post: http://stackoverflow.com/questions/101265/why-is-there-not-a-foreach-extension-method-on-the-ienumerable-interface . You can use it on a Dictionary, but then your action method should receive a KeyValuePair (an IDictionary(Of TKey, TValue) is an IEnumerable(Of KeyValuePair(Of TKey, TValue)). You still have to stop running and recompile to add that method, of course. – Omer Raviv Apr 30 '11 at 14:28
  • @Omer Raviv - If you're doing 32 bit development, then you could use edit and continue to add the method without stopping debugging. – Erik Funkenbusch Apr 30 '11 at 21:43
2

No, you can't do this directly from the IDE. Unfortunately, the easiest way to work around it is to stop debugging, write your loop inside a public static method that returns the object you are looking for, re-compile and run, and then call that public static method from the Immediate or Watch window.

Another more immediate (but annoying) trick is to write "? myObjects" in the Immediate window, copy paste the result into notepad, and use text search (Ctrl+F) in notepad to find your object.

Omer Raviv
  • 11,409
  • 5
  • 43
  • 82
  • Thanks. Is there a way to get `? myObjects` to give me more than 100 records? Also, the collection in question is a dictionary with values of another class, which are coming up as `MyApp.MyClass` without any relevant info - is there a workaround for this? – Flash Apr 30 '11 at 08:34
  • First question - Not that I'm aware of, sorry. You can copy paste from the Watch window into notepad, too. Second question - yes, you should put a [DebuggerDisplay("ID: {id}")] over MyClass. For more information, see http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx – Omer Raviv Apr 30 '11 at 08:46