How to set conditional breakpoints in Visual Studio?

前端 未结 13 2172
忘了有多久
忘了有多久 2020-11-27 03:35

Is there an easy way to set conditional breakpoints in Visual Studio?

If I want to hit a breakpoint only when the value of a variable becomes something, how can I do

相关标签:
13条回答
  • 2020-11-27 04:05

    Create a breakpoint as you normally would, right click the red dot and select "condition".

    0 讨论(0)
  • 2020-11-27 04:05

    If you came from Google, this answer might be what you are searching for.

    1. Click Debug> New BreakPoint > Function Breakpoint

    2. there choose the conditional Breakpoint.

    0 讨论(0)
  • 2020-11-27 04:07

    Just another way of doing it, (or if you are using express) add the condition in code:

    if(yourCondition)
    {
        System.Diagnostics.Debugger.Break();
    }
    
    0 讨论(0)
  • 2020-11-27 04:12
    1. Set a breakpoint as usual
    2. Right click on the breakpoint and select Condition
    3. You'll see a dialog that says "Breakpoint Condition"
    4. Put a condition in the field e.g. "i==5"

    The breakpoint will only get hit when i is 5.

    0 讨论(0)
  • 2020-11-27 04:15
    1. Set breakpoint on the line
    2. Right clik on RED ball
    3. Chose conditioal breakpoint
    4. Setup condition
    0 讨论(0)
  • 2020-11-27 04:15

    Writing the actual condition can be the tricky part, so I tend to

    1. Set a regular breakpoint.
    2. Run the code until the breakpoint is hit for the first time.
    3. Use the Immediate Window (Debug > Windows > Immediate) to test your expression.
    4. Right-click the breakpoint, click Condition and paste in your expression.

    Advantages of using the Immediate window:

    • It has IntelliSense.
    • You can be sure that the variables in the expression are in scope when the expression is evaluated.
    • You can be sure your expression returns true or false.

    This example breaks when the code is referring to a table with the name "Setting":

    table.GetTableName().Contains("Setting")
    
    0 讨论(0)
提交回复
热议问题