469,271 Members | 1,251 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

variable value change breakpoint

Hi,

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

Thank you,
Max
Feb 8 '06 #1
5 4806

Maxwell2006 wrote:
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

Feb 8 '06 #2
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. :)

Feb 8 '06 #3

Bruce Wood wrote:
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

Feb 8 '06 #4
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.

Feb 8 '06 #5

Bruce Wood wrote:
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

Feb 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Varadha | last post: by
2 posts views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
5 posts views Thread by aamirghanchi | last post: by
5 posts views Thread by ryanwpenfold | last post: by
10 posts views Thread by Mason Barge | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.