mjsr
mjsr

Reputation: 7590

How can i stop the debugger in a particular variable value?

how can i set de debugger to stop when some particular variable has a defined value?. For example i have a code that it crash that loops 10000 time to make some postprocessing. I know that the error could be produced from the 7000 iteration up, so i want to stop from there on, avoiding manual loop from the first 7000. Im using visual studio 2008 and 2010 with c#, i think that the solution will be the same for both.

Upvotes: 33

Views: 20592

Answers (3)

JaredPar
JaredPar

Reputation: 755557

What you're looking for is a conditional break point. Here's how to set it up assuming the variables name is i.

  • Set a normal breakpoint on the line after the value is set
  • Right click on the red dot portion of the breakpoint and select "Condition"
  • Enter the condition which you want to check for. Example: i == 10000
  • Hit OK

Now run your scenario again and the breakpoint will be hit only when the value of i equals 10000.

A word of warning. You can put pretty much any legal C# expression into a conditional break point but it will be evaluated every single time the break point is hit. That can lead to very slow debugging if use a complex conditional

Upvotes: 66

sbi
sbi

Reputation: 224189

Open the breakpoint window and create a new data breakpoint from its menu.

Upvotes: 0

Alvaro Ardila
Alvaro Ardila

Reputation: 372

The easy way to do that is:

if (nameVariable = X ) { BreakPoint: nameVariable; }

Upvotes: -1

Related Questions