IDE Debugger enhancements (type viewer attributes and type visualizers) by Luca Dellamore

IDE Debugger enhancements (type viewer attributes and type visualizers) by Luca Dellamore

  • Comments 3

In Visual Studio 2003 it was sometimes difficult to examine large complicated objects in the debugger (since it is simply impossible for any debugger to analyze arbitrary objects and figure out what the developer is really interested in knowing).

To improve this scenario in Visual studio 2005 we introduced the concept of attributed debugging, that allows the developer of the type to also specify what that type will look like when it’s being debugged.

 

DebuggerDisplay Attribute

The DebuggerDisplay attribute let us choose how we want to display a type in the debugger: take for example the 2 following variables.

 

    Dim overflow As New OverflowException
    Dim myPoint As New System.Drawing.Point(x:=3, y:=4)

 

In Visual Studio 2003 when you added them to the watch window this was what you would see:

Check out instead the new Visual Studio 2005 view: much more information without having to drill down the TreeView.

 

This is a simple example to show you the syntax:

 

<DebuggerDisplay("Employee: {FullName}")> Public Class Employee
    Public FirstName As String
    Public LastName As String
    Public Salary As Decimal
    Public JobTitle As String
    Public ReadOnly Property FullName() As String
        Get
            Return FirstName & " " & LastName
        End Get
    End Property
End Class

 

Notice that FullName is inside curly brackets: this means that Visual Studio will evaluate the expression and in this case it will display the return value of the Property FullName.

So if you run the following code:

 

'DebuggerDisplay attribute
Dim emp As New Employee
emp.FirstName = "Thomas"
emp.LastName = "Andersen"
emp.Salary = 65000
emp.JobTitle = "Software Development Engineer"

 

 

and in debug mode you hover over your variable ‘emp’ you will see the following tip (that gives you more information at a glance that simply the class name)

DebuggerTypeProxy Attribute

 If we want to have even more control of display of a type at debug time we can use the DebuggerTypeProxy attribute that let us specify a display proxy for the type:

 

<DebuggerTypeProxy(GetType(Employee.View))> <DebuggerDisplay("Employee: {FullName}")> _
Public Class Employee
    Public FirstName As String
    Public LastName As String
    Public Salary As Decimal
    Public JobTitle As String
    Public ReadOnly Property FullName() As String
        Get
            Return FirstName & " " & LastName
        End Get
    End Property
    'this is the Proxy class that will be shown at runtime
    Friend Class View
        Private privateInstance As Employee = Nothing
        Public Sub New(ByVal a As Employee)
            Me.privateInstance = a
        End Sub
        Public ReadOnly Property JobTitle() As String
            Get
                Return privateInstance.JobTitle
            End Get
        End Property
        Public ReadOnly Property FullName() As String
            Get
                Return privateInstance.FullName
            End Get
        End Property
    End Class 'View
End Class

 

Now the Employee class will display like so:

Note that only public members of the Proxy class are shown (unless you change the option: ToolsàOptionsà Debuggingà Generalà Show all members for non-user objects in variable window (Visual Basic only))

Note that if you are debugging in your project you will see also private members: the picture above was taken when using a dll reference.

This attribute is used for example in some Framework classes like the Hashtable.

If you want to see the real structure of a type and not the proxy class you can go to

Toolsà Optionsà Debuggingà General and check the box: Show raw structure of objects in variables window

 

Type Visualizers

My favorite new enhancement is the introduction of type visualizers: some types (for example the Dataset) simply are not easily viewable in the watch window: that is why we have now in Visual Studio the option to create an entirely new view of the data.

Run the following code that will create a simple dataset, add ‘ds’ to the watch window and click the magnifying glass to view the DataSet visualizer (btw: you can also edit the data of your DataSet in the Visualizer)

 

   Dim ds As New DataSet
   ds.Tables.Add(New DataTable("Friends"))
   ds.Tables(0).Columns.Add("Name", GetType(String))
   ds.Tables(0).Columns.Add("Age", GetType(Integer))
   ds.Tables(0).Rows.Add(New Object() {"Marc", 14})
   ds.Tables(0).Rows.Add(New Object() {"Luca", 12})
   ds.Tables(0).Rows.Add(New Object() {"Jessica", 9})
   Dim xmlString As String = ds.GetXml

If you add the ‘xmlString’ variable form the previous code snippet to the watch window you can try also the 3 default visualizers for the String type: the following is a picture of the XML Visualizer (no more cut and paste of long strings into notepad).

If you want to see how to create a very simple Visualizer you can find a VB example in Deep’s blog (Deep is a friend that works in the C# group but he included also a sample in VB in his post: thanks Deep for thinking about us too)

http://blogs.msdn.com/deeptanshuv/archive/2005/03/21/400194.aspx
 
Happy debugging!

Luca Dellamore

Visual Basic Test Team

 

Leave a Comment
  • Please add 2 and 6 and type the answer here:
  • Post
  • Nice feature !!!
    I just wrote a spanish Article based on this post ... I'll put the link when I have !!
    Great work !!!

    Bye from Spain.
  • Great feature. But I don't get the "ItemDescription" property value on the first debug hover line - I get the variable name followed by the tyep in curly brackets. I must expand the plus sign (+) by hovering over it on the firstdiaplyed line and then my property value displays on the second debug hover line. Any ideas why?

    My class definition line is:
    <DebuggerDisplay("SPSFoundItemInfo: {ItemDescription}")> Public Class SPSFoundItemInfo

    ItemDescription is a ReadOnly property of the class and returns a value as expected.
  • Sorry but I could not reproduce your issue: I tried the code below and it works fine:
    If I hover hover over the variable AddMeToWatch I see: SPSFoundItemInfo: "MyDescription"

    Module Module1
    Sub Main()
    Dim AddMeToWatch As New SPSFoundItemInfo
    Stop
    End Sub
    End Module
    <DebuggerDisplay("SPSFoundItemInfo: {ItemDescription}")> Public Class SPSFoundItemInfo
    Public ReadOnly Property ItemDescription() As String
    Get
    Return "MyDescription"
    End Get
    End Property
    End Class
Page 1 of 1 (3 items)