How do I “run until this variable changes” when debugging?

前端 未结 8 777
醉话见心
醉话见心 2020-12-30 23:29

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

相关标签:
8条回答
  • 2020-12-30 23:51

    Simple trick for Express edition:

    private string myValue;
    public string MyValue
    {
      set
      {
        if (this.myValue != value) Debugger.Break();
        this.myValue = value;
      }
    }
    
    0 讨论(0)
  • 2020-12-30 23:55

    you can use conditional breakpoints

    see this

    0 讨论(0)
  • 2020-12-30 23:57

    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.

    0 讨论(0)
  • 2020-12-31 00:00

    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.

    0 讨论(0)
  • 2020-12-31 00:00

    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

    0 讨论(0)
  • 2020-12-31 00:02

    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.

    0 讨论(0)
提交回复
热议问题