Ahmad
Ahmad

Reputation: 13406

Any way to set a value dependent breakpoint?

I want to know, is there any way in Visual Studio 2008 to set a value breakpoint kind of a thing ? Like say there's a variable called 'test', and I want to the code to stop at any line in the entire project where the value of this variable is being changed .. ?

That is, I don't want any line specific breakpoint .. I just want Visual Studio to stop at the line of code where a change is being made to some variable ..

The code I have is very complex and it would be a lot easier for me to debug the code if I can get the mentioned functionality somehow ..

Upvotes: 1

Views: 499

Answers (3)

Khanzor
Khanzor

Reputation: 5000

You use a conditional breakpoint, and in the values, type in the variable name, and change the checkbox from "Is true" to "Has changed".

To set this up, left click on the left hand column where breakpoints appear. A breakpoint will be created.

You then right click on the red glyph that appeared, and select condition from the drop down menu.

This should get you where you need to be.

Upvotes: 0

Andrew Kennan
Andrew Kennan

Reputation: 14157

You should encapsulate the variable in a property so the field named test becomes _test or mTest or whatever and you create a new property called test that other code will use. You can put the breakpoint on the setter of the property.

So instead of having

public int test;

You can have

private int _test;

public int test
{
  get { return _test; }
  set { _test = value; } // Breakpoint goes here.
}

And there's no need to alter any of the users of test, unless they've already been compiled in which case you'll need to compile them again.

Upvotes: 3

RPM1984
RPM1984

Reputation: 73112

Yep, use a Conditional Breakpoint.

Set a breakpoint to the line of code you want to debug.

Then right click on the red circle (breakpoint), click "Condition".

Then set the condition that you want to be true in order to breakpoint. (e.g break when "test" == 1).

Keep in mind - you still have to set the breakpoint somewhere, as it needs to have scope of the variable used in the condition.

HTH.

Upvotes: 3

Related Questions