Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "run until this variable changes" when debugging?

Tags:

When debugging my C#, I often want to know when a variable's value changes and then investigate the state of the program.

Currently, I do it like this:

  1. Watch-list the offending variable.
  2. Physically spam F10 (shortcut for Step Over) until I see the value change.

However, the number of F10s required is annoying.

Surely this has been automated, I thought. But I cannot find this feature in my Microsoft Visual C# Express, which surprises me. After all, the Watch-list does automatically highlight changed values in bright red.

Am I missing something?

like image 379
Anko Avatar asked Jun 13 '11 16:06

Anko


People also ask

How can the value of a variable be modified while debugging?

You can change a variable in the Evaluate dialog or in the Watch List panel. The Evaluate dialog: To invoke the dialog, press the Call the Evaluate Dialog button on the Debug toolbar, or press the Ctrl+F12 shortcut (this is the default shortcut. You can change it any time.

How do I run code in debug mode?

Press F5 and hover over the type variable again. Repeat this step until you see a value of I in the type variable. Now, press F11 (Debug > Step Into or the Step Into button in the Debug Toolbar). F11 advances the debugger (and executes code) one statement at a time.

How can I see the variable value while debugging in Visual Studio?

When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable. If the variable is an object, you can expand the object by clicking on the arrow to see the elements of that object.


2 Answers

Simple trick for Express edition:

private string myValue; public string MyValue {   set   {     if (this.myValue != value) Debugger.Break();     this.myValue = value;   } } 
like image 69
Teoman Soygul Avatar answered Oct 04 '22 21:10

Teoman Soygul


Conditional breakpoints are what you're after, but it appears they are not available in Visual Studio Express.

In Visual Studio (non-Express), the way it works is that you create a conditional breakpoint with an expression equal to your watch value and "Has changed" as your breakpoint condition.

like image 37
Ben Hoffstein Avatar answered Oct 04 '22 21:10

Ben Hoffstein