开发者

Follow an object state in Visual Studio debug mode

Say I have a WinForm, in whic开发者_如何学Ch I have a private int selectedObjectsCount.

Is there a way in debug mode to "follow" this object state through the debug session?

Or, other case, I have a myFont instance. I want to see in what moment its property IsBold changes (is modified)

I need a trace, because an object became "bad valued" and I don't understand where that happens.

Is something similar possible in VS 2010?


Make a breakpoint wherever suitable to see when object changes, right click breakpoint and select "When hit..." here you can print the object content in a debug window -- effectively seeing when the object changes and to what.

At any breakpoint you will have a full trace in the "Call stack" debug window.

The art of debugging is something you should value and learn (it will pay off). See this page for a very simple overview of debugging alternatives in VS.


In such case I implement selectedObjectsCount as property and in setter I do check on value changing:

set
{
    if (M_selectedObjectsCount != value)
    {
        selectedObjectsCount = value;//break here
    }
}


You can use Watch window. If you don't see it in bottom of your VS, you can turn it on by going to Debug->Windows->Watch->Watch 1 (or other Watch windows). This menu item is only available while you are debugging.

You can add objects, their properties and even expressions there, and watch their values while you trace. Items are added by double clicking last row in Watch window and entering your expression in Name column.

E.g. enter Height in name while you're debugging inside a form and you'll see it's value. You can also do things like, (Height + Width) / 2.

You cannot however set breakpoint on value change. But while tracing, row who's value is changed will get highlighted. Unfortunately, you have to step through code, you cannot just run it.


Are you an ex-Visual Interdev/VB6 user?

I remember that you could set a breakpoint to "break when value changes". Unfortunately that's not possible with subsequent versions.

I think @TheNIK's idea of replacing with a property is probably you're best bet. Or using 'view all references' and then doing a print "When hit" as @TeddHansen says.


As other have stated, there are no "Data Breakpoints" in .NET. The best you can do, in case you own the variable you want to track, is to put a breapoint inside the setter, then right click the breakpoint, choose Conditional Breakpoint to check that indeed the value is changing. In case the variable you want to track is a field, you'd have to convert it to a property.

In case you don't own the code of the property, you can use Resharper's Value Origin feature to quickly find all the places where the property is being set by your code, and put breakpoints there: a lot of work but it gets the job done.


It is possible to do this in Visual Studio 2010. What you need to use is the 'Debug/New Breakpoint/New Data Breakpoint...' menu option.

However, the option is only available for native C++ projects.

For more information, see here - http://msdn.microsoft.com/en-us/library/350dyxd0.aspx

[edit] - For other languages and your specific situation, you could create a new class which inherited from the Font class and override the IsBold property. Put your breakpoint in there and use this new custom class instead of Font. It's a bit long winded, but should work.


Technically there are no "Data breakpoints" in C# since the objects move in memory during the Garbage collections compact phase. But often you can do this trick with properties - properties are compiled into setter and getter methods, and you can set breakpoint on them just like any other method.

I have just answered a very similar question here:

Reasking about hitting breakpoint at property setter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜