By MrClan


2011-07-12 20:08:13 8 Comments

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 it?

12 comments

@JAiro 2011-07-12 20:11:00

Create a conditional function breakpoint:

  1. In the Breakpoints window, click New to create a new breakpoint.

  2. On the Function tab, type Reverse for Function. Type 1 for Line, type 1 for Character, and then set Language to Basic.

  3. Click Condition and make sure that the Condition checkbox is selected. Type instr.length > 0 for Condition, make sure that the is true option is selected, and then click OK.

  4. In the New Breakpoint dialog box, click OK.

  5. On the Debug menu, click Start.

@Andy Joiner 2015-05-12 21:30:00

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")

@C Johnson 2011-07-12 20:23:43

Visual Studio provides lots of options for conditional breakpoints:

To set any of these you

  1. Set a breakpoint.
  2. Right-Click over the breakpoint, and in the popup menu you select an option that suites you.

These options are as follows:

  • You can set a condition, based on a code expression that you supply (select Condition from the popup menu). For instance, you can specify that foo == 8 or some other expression.
  • You can make breakpoints trigger after they have been hit a certain number of times. (select Hit Count from the popup menu). This is a fun option to play with as you actually aren't limited to breaking on a certain hit count, but you have options for a few other scenarios as well. I'll leave it to you to explore the possibilities.
  • You can Set filters on the Process ID, thread ID, and machine name (select Filter from the popup menu)

@Vorac 2013-04-08 06:50:10

On Visual Studio 6.0

Alt+F9!!!

@Tim Medora 2013-04-08 06:55:33

-1) This doesn't work in VS 2012 2) This question already has plenty of other (better) answers and 3) who on earth cares about VS 6.0 (released in 1998)? 4) Even if someone cares about VS 6, the question is tagged with .Net which didn't have support until the next version of VS (Visual Studio .Net)

@Vorac 2013-04-10 06:18:24

Well, the other answers do not work on VS 6.0. So I posted a solution.

@Ignacio Soler Garcia 2014-05-28 11:46:58

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

if(yourCondition)
{
    System.Diagnostics.Debugger.Break();
}

@Brandon Moretz 2011-07-12 20:10:49

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

@panako 2013-11-18 11:10:23

When you are using Express edition you can try this:

#if DEBUG
    if( fooVariable == true )
        System.Diagnostics.Debugger.Break();
#endif

if statement makes sure that in release build breakepoint will not be present.

@Goldfish Sandwich 2014-05-07 13:49:09

This has a great performance/time-saving advantage. I was iterating through every potential int 'id' in a program of mine (only about 3.5 million), which only takes a couple of seconds. I was trying to use the conditional breakpoint but since it wasn't in a conditional branch it slowed down the execution speed to the point where after a few minutes I would just terminate the debug session. Using the compiled breakpoint made it instantaneous.

@Casey 2011-07-12 20:13:42

  1. Set a breakpoint as usual.
  2. Right-click on the breakpoint marker
  3. Click "Condition..."
  4. Write a condition, you may use variable names
  5. Select either "Is True" or "Has Changed"

@FIre Panda 2011-07-12 20:12:23

Set the breakpoint as you do normally, right click the break point and select condion option and sets your condition.

@user423119 2011-07-12 20:11:44

  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.

@Tigran 2011-07-12 20:10:25

  1. Set breakpoint on the line
  2. Right clik on RED ball
  3. Chose conditioal breakpoint
  4. Setup condition

@Jeff 2011-07-12 20:10:05

Set a breakpoint as usual. Right click it. Click Condition.

@mark.monteiro 2015-12-08 14:29:18

In VS 2015 you need to hover over the breakpoint and click the gear icon to set conditions

@Toby Caulk 2019-08-01 15:11:18

You mean, right click, set condition, get "Condition for a breakpoint failed" error.. didn't you?

@Andrew 2019-11-01 18:35:51

Answer should also include syntax for conditional statements, or an informative link.

Related Questions

Sponsored Content

46 Answered Questions

[SOLVED] Deep cloning objects

  • 2008-09-17 00:06:27
  • NakedBrunch
  • 773121 View
  • 2126 Score
  • 46 Answer
  • Tags:   c# .net clone

29 Answered Questions

[SOLVED] How to enumerate an enum?

28 Answered Questions

[SOLVED] Get int value from enum in C#

  • 2009-06-03 06:46:39
  • jim
  • 1411630 View
  • 1692 Score
  • 28 Answer
  • Tags:   c# enums casting int

12 Answered Questions

[SOLVED] Can you force Visual Studio to always run as an Administrator in Windows 8?

25 Answered Questions

[SOLVED] Error message "No exports were found that match the constraint contract name"

  • 2013-07-11 14:47:24
  • Simon B.Robert
  • 314088 View
  • 1056 Score
  • 25 Answer
  • Tags:   visual-studio

17 Answered Questions

[SOLVED] How do I "Add Existing Item" an entire directory structure in Visual Studio?

  • 2008-09-11 22:09:48
  • Ian Patrick Hughes
  • 242636 View
  • 758 Score
  • 17 Answer
  • Tags:   visual-studio ide

16 Answered Questions

[SOLVED] Using Git with Visual Studio

  • 2009-02-03 14:46:19
  • Herb Caudill
  • 468802 View
  • 1451 Score
  • 16 Answer
  • Tags:   visual-studio git

4 Answered Questions

[SOLVED] Is there a reason for C#'s reuse of the variable in a foreach?

Sponsored Content