faya
faya

Reputation: 5725

Is there a tool for Visual Studio to track (or break on) variable value?

Is there is a tool or a setting in the Visual Studio debugger to stop on breakpoints or when a variable is set to a particular value? I mean, if I know that value will be set to "HELLO," I want the debugger will stop the same way it would if it reached a breakpoint?

Upvotes: 4

Views: 2368

Answers (5)

smile.al.d.way
smile.al.d.way

Reputation: 361

try - System.Diagnostics.Debug.Assert(yourVariable <> "HELLO") and then click on 'Cancel' button to start debugging. This works for ASP.net and Silverlight projects

Upvotes: 0

toto
toto

Reputation: 900

Daves answer.

And I'll add that you can just add a if statement that contains a couple of dummy statements and you put a breakpoint inside it. It does the same thing.

Typical use :

if (i == 250) {
 int dummy = 2+2;  //breakpoint here
}

In your case, since you watch the value of a string (assuming C++ strings)

if (mystring == "hello")
{
  int dummy = 2+2; //breakpoint here
}

Upvotes: 3

Brian R. Bondy
Brian R. Bondy

Reputation: 347566

  1. Set a breakpoint anywhere in code.
    • Enable the list of breakpoints window by going to Debug menu -> Windows -> Breakpoints.
    • In your breakpoints window, right click on a breakpoint
    • Select Condition...
    • Enter any expression involving your variable

The breakpoint will be hit when the condition is met.

Via the right click on breakpoints menu, you can also set breakpoints:

  • Only from certain processes or threads
  • Upon hit counts
  • Only when a condition or variable is changed

Upvotes: 8

Dave Swersky
Dave Swersky

Reputation: 34820

You're looking for a Conditional Breakpoint.

Upvotes: 9

bhups
bhups

Reputation: 14895

there are watchpoints.

Upvotes: 4

Related Questions