Welcome to EMC Consulting Blogs Sign in | Join | Help

Howard van Rooijen's EMC Consulting Blog (2004 - 2010)

This blog has now moved to http://howard.vanrooijen.co.uk/blog - please update your subscriptions if you wish to receive new content.

Improve your Debugging Experience by using the DebuggerDisplayAttribute

Visual Studio 2005 offered a number of User Experience improvements within the Debugging feature set – the big furore in the lead up to release was that VB.NET developers would get Edit & Continue support but C# Developers wouldn’t. This decision was soon reversed after Microsoft had an Obi-Wan moment (and sensed “a great disturbance in the Force, as if millions of voices suddenly cried out in terror”). The noise generated by this meme blocked out some of the other great debugging features that were added in VS 2005 – such as Debugger Visualizers and the DebuggerDisplayAttribute, which bang for buck is the easiest way to improve your debugging experience.

To demonstrate the DebuggerDisplayAttribute – I’ll reuse the code from my previous post: we start with a normal POCO class:

[ImplementINotifyPropertyChanged]
[ExcelSheet(Name = "Deceased MPs")]
public class MemberOfParliamentDataModel
{
    [ExcelColumn(Name = "constituency")]
    public string Constituency { get; set; }

    [ExcelColumn(Name = "firstname")]
    public string FirstName { get; set; }

    [ExcelColumn(Name = "fromdate")]
    public string FromDate { get; set; }

    [ExcelColumn(Name = "fromwhy")]
    public string FromWhy { get; set; }

    [ExcelColumn(Name = "lastname")]
    public string LastName { get; set; }

    [ExcelColumn(Name = "party")]
    public string Party { get; set; }

    [ExcelColumn(Name = "todate")]
    public string ToDate { get; set; }

    [ExcelColumn(Name = "towhy")]
    public string ToWhy { get; set; }
}

and then we use the LINQ to Excel  provider to perform a LINQ query:

var xLabourMps = from xMp in xMps 
                 where xMp.Party == "Lab" 
                 select xMp;

We can add a break point on the next line where we iterate through the collection to print out the results. If you expand the debugger data tips for the “Results View”  property you see a long list of LINQtoExcel.Sample.DataModel.MemberOfParliamentDataModel objects. If you expand one of those nodes then you get to see the actual values of the objects. Very powerful, but ever so slightly clunky.

image

Enter the DebuggerDisplayAttribute:

To improve the debugging experience all we need to do is decorate our Data Model object with a DebuggerDisplayAttribute, and list the object’s properties you want to display within { } as follows:

[System.Diagnostics.DebuggerDisplay("{FirstName} {LastName} - {Party} Party")]
[ImplementINotifyPropertyChanged]
[ExcelSheet(Name = "Deceased MPs")]
public class MemberOfParliamentDataModel
{

}

Now when we next run the application and hit the breakpoint, we get a much more relevant data tip:

image

This one line of code will save you numerous mouse clicks and if you’re trying to become a better developer using a continuous improvement cycle, adopting a Lean mindset of Eliminating Waste realistically means making a large number of small adjustments to your everyday working habits. Work smarter, not harder.

@HowardvRooijen

Published Friday, August 28, 2009 9:14 AM by howard.vanrooijen

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

jamie.thomson said:

Very very cool. Thanks for this mate - I never had a clue!!

August 28, 2009 10:26 AM
 

Daren said:

I have always added an override of ToString to achieve similar ends... Useful in other scenarios also. Good tip though, for sure.

August 28, 2009 6:42 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication

News

This blog has now moved - please visit http://howard.vanrooijen.co.uk/blog for new content!
Add to Live.com
Powered by Community Server (Personal Edition), by Telligent Systems