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.
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:
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