24

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?

8 Answers8

22

Simple trick for Express edition:

private string myValue;
public string MyValue
{
  set
  {
    if (this.myValue != value) Debugger.Break();
    this.myValue = value;
  }
}
Blue
  • 22,608
  • 7
  • 62
  • 92
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • 1
    At the moment, parts of my code deliberately change the variable directly -- without going trough the setter. Also, the setter already does many things besides set the value. If I made separate setters for "setter-direct" and through-setter access, the field would still be directly accessible from the class. – Anko - inactive in protest Jun 13 '11 at 17:05
6

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.

Ben Hoffstein
  • 102,129
  • 8
  • 104
  • 120
  • 24
    Not really. I assume the OP want a sort of _data breakpoint_, which is triggered whenever anything touches the given memory location. Such kind of breakpoints are AFAIK not available in Visual Studio. In contrast, conditional breakpoint is triggered if the data is changed and the code is at some _predefined_ location. – Vlad Jun 13 '11 at 16:43
  • @Vlad Correct me if I'm wrong but they are possible but not with C# to be exact. – jeromej Sep 14 '18 at 11:08
  • 1
    @JeromeJ: The problem with C# is the compacting garbage collector which can move the data in memory anytime, so the debugger ought to move the data breakpoints accordingly during the compaction phase (which seems to be quite tedious task to implement, but not impossible). Unmanaged languages don't have this problem, so data breakpoints [are easier there](https://sourceware.org/gdb/onlinedocs/gdb/Set-Watchpoints.html). – Vlad Sep 19 '18 at 18:24
2
  1. Set the breakpoint where you want the code to pause
  2. Open the Breakpoints window (Debug -> Windows -> Breakpoints)
  3. Right-click on your breakpoint and select Condition...
  4. Set the condition to be the variable name, and select the Has Changed radio button.
  5. Click OK, and then debug as per normal :)
Darth Android
  • 3,437
  • 18
  • 19
  • So many years after, now in VS 2019 and 2022, this is the way to do Conditional breakpoint. Now, entry edition is community, and it functions as a charm. – Marcelo Scofano Diniz Oct 21 '21 at 01:27
0

Here's how I do it in Visual Studio. Set a breakpoint by pressing F9 or clicking in the very left margin. Then right click over the red dot that appears and select the Condition command. There are other options there as well.

However, this may not be supported in Visual Studio Express.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

you can use conditional breakpoints

see this

Illuminati
  • 4,539
  • 2
  • 35
  • 55
0

You could write an if statement that checks for a change and have a break point happen within that if statement, thus it breaks initially, then you click resume, it continues until it hits this break point.

Remm
  • 677
  • 8
  • 24
0

Use Debugger.Break based on some runtime condition, or go rightclick on some breakpoint, and choose conditional break -> has changed

Edit: dunno about this in Express

Denis Biondic
  • 7,943
  • 5
  • 48
  • 79
0

If you want to catch variable change at some breakpoint, as opposed to catching exactly where it was changed, then set the breakpoint, right-click it and choose "Condition". Let's say your variable name is X and it's current value is A. Enter "X != A" in the condition field.

Now the breakpoint will only be hit after X changes to some value other than A.