Geeks With Blogs
Darren Fieldhouse

Attributes are a great way to communicate some extra information about a class, method or property and the debugger looks for a few different attributes to determine the way it behaves while you are debugging code. Let’s take a look at the attributes that are used to control how the data tips display an object’s value.

Imagine you had a simple customer class. Here is a screenshot of it at runtime:

 

Debugger with no attributes

As you can see, the debugger is showing the public properties and the private fields that are used to store them. There are times that can be extremely useful, but in this case it’s just noise. We can simplify the display using the DebuggerBrowsable attribute with a parameter of DebuggerBrowsableState.Never

<DebuggerBrowsable(DebuggerBrowsableState.Never)> _
Private _Name As String

Having added the attribute to both the _Name and _Orders fields, this is what we see in the data tip:

CustomerBrowsableAttributesThe next thing I want to do is change what is displayed when we first hover over the class. Currently the debugger displays the type name ( DebuggerDemo.Main.Customer), but it would be much more useful it we saw the customer name. The DebuggerDisplay attribute applied at the class level can be used to display any string, including the values of properties or the results of method calls. The Name property could be displayed like this:

<DebuggerDisplay("Customer: {Name}")> _
Public Class Customer

The curly brackets are used to identify expressions within the literal string. Applying the above attribute to the Customer class gives a much more useful summary of the object:

CustomerDisplayAttribute

The quotation marks that are automatically applied to any string value can be removed by adding nq to the expression, i,e,:

<DebuggerDisplay("Customer: {Name,nq}")>

Be careful, if you put a space after the comma it won’t work! The DebuggerDisplay attribute can be used to display complex expressions if needed, for example, we might want to add some details about the customer’s orders to the data tip:

<DebuggerDisplay("Customer: {Name,nq} ({If(Orders.Count>0, Orders.Count.ToString(), ""no""),nq} orders)")> 

(This should all be on one line, but I haven’t added underscores because they would be mid-string, and that would get too confusing).

The above attribute would make our customer data tip appear like this:

CustomerDisplayAttribute2The final thing I want to demonstrate uses the  DebuggerBrowsable attribute again, except this time it is with the DebuggerBrowsableState.RootHidden parameter. By decorating the Orders property with this attribute we can auto-expand the list of orders (or more accurately, hide the root and display all the children instead). This is the code I added:

<DebuggerBrowsable(DebuggerBrowsableState.RootHidden)> _
Public Property Orders() As List(Of Order)

And this is the result:

 CustomerBrowsableAttribute2

Of course, I also added a DebuggerDisplay attribute to the Order class so we see information about each order. That was achieved with this attribute:

<DebuggerDisplay("Order: {Product,nq} x {Qty}")> _
Public Class Order

You are not going to want to apply these attribute to every class you write, but for some of your main class that you are debugging with all the time it is worth the effort. In fact, there have been times I’ve added a DebuggerDisplay attribute to a class just to debug it, removing it when I was finished!

kick it on DotNetKicks.com Posted on Sunday, March 8, 2009 12:25 PM | Back to top


Comments on this post: Debugging Attributes

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © DarrenFieldhouse | Powered by: GeeksWithBlogs.net