variable value change breakpoint

  • Thread starter Thread starter Maxwell2006
  • Start date Start date
M

Maxwell2006

Hi,

Can I have a breakpoint that stop execution as soon as any code change the
value of a specific variable?

Thank you,
Max
 
Maxwell2006 said:
Hi,

Can I have a breakpoint that stop execution as soon as any code change the
value of a specific variable?

Thank you,
Max

Absolutely. Just go to Debug | Breakpoints and add a new one. Go to the
Data tab and enter your variable and condition, if you like. If you
just want
anytime it changes, select Condition and select has changed.

Of course, you DO understand this will really slow down your debugging.

Matt
 
No, you cannot. The VS debugger does not have this capability.

Matt has given you the best solution available: break at a certain
point in the code only if a certain condition is true.

But no, you cannot "stop as soon as this variable's value changes, no
matter what piece of code changes its value". The only way to do this,
really, is to find every place where that variable is changed and place
breakpoints at all of those places.

This is also a good argument against public fields. :)
 
Bruce said:
No, you cannot. The VS debugger does not have this capability.

Actually, it does. Just not for C#. I'd done it so many times in C++
I didn't realize it didn't work for C#.
Matt has given you the best solution available: break at a certain
point in the code only if a certain condition is true.

But no, you cannot "stop as soon as this variable's value changes, no
matter what piece of code changes its value". The only way to do this,
really, is to find every place where that variable is changed and place
breakpoints at all of those places.

This is also a good argument against public fields. :)

Its kind of sad, really. Although, if you simply used a property in
place
of a field, and set the breakpoint in the setter, it would accomplish
the
same thing.

Matt
 
I didn't realize that the debugger can do this for C++.

I just wanted to point out, though, that the solution in your earlier
post is _not_ what the OP was looking for. You told him how to set a
breakpoint at a particular spot with a condition on it. You can do that
in C#, too. I do it all the time, but it's not at all the same thing as
what the OP wanted to do.

Setting a conditional breakpoint says, "Stop _here_ only if this
variable has this value."

Breaking on a change to a variable says, "Stop _anywhere_ that changes
this variable to this value."

Not the same thing at all. The second one has no particular break
location: it's a break condition floating out in space that doesn't
belong to any particular place in the code until the change occurs.
Can't do that in VS, at least not in C#. Can you really do it in
managed C++? If so, I wonder why they haven't included it in the C#
debugger.

Your second solution is the right one: if it's a public field, make it
a property, and then set the breakpoint in the setter and anywhere else
within your class that sets the now-private field directly. In C#,
there really is no other way.
 
Bruce said:
I didn't realize that the debugger can do this for C++.

I just wanted to point out, though, that the solution in your earlier
post is _not_ what the OP was looking for. You told him how to set a
breakpoint at a particular spot with a condition on it. You can do that
in C#, too. I do it all the time, but it's not at all the same thing as
what the OP wanted to do.

That's correct. But, in fact, in VS.NET, you *can* set a breakpoint
that says "stop when the variable changes". Its a radio button under
the
Condition dialog. It just doesn't work in C#.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top