Click here to Skip to main content
15,884,177 members
Articles / Visual Studio
Tip/Trick

Showing Method Parameter Values in Callstack Window

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
22 Apr 2017CPOL2 min read 10.7K   1
An easier way to show parameter values of chain of methods from Visual Studio

Many times, when you are debugging your code deep inside chain of methods, you might need to know the value of simple or complex objects passed as method arguments. If you have a breakpoint set in a method, then you can always go back to caller method and just inspect the values. However, this could be a cumbersome approach and there is an easier way.

Let’s take a look at an example. Let’s say we are debugging a program and have the following call stack available in Visual Studio.

Image 1

At this point, code is stopped at the ExtractAmount method that has a float type cost argument. In addition, caller of this method Pay has PaymentInfo and float type objects as arguments. Finally, the caller of Pay method Process has Order and Customer as arguments. If we want to see the values of the arguments of all methods involved, we can just right click on the Callstack window that it will bring the following pop-up dialog with an option to “Show Parameter Values”.

Image 2

If we select this option now, the Callstack will look as follows:

Image 3

It's better than before as we can see the simple data type values (float in this case) but the complex object type values are not showing any useful values. The reason is that by default, debugger shows the type of an object as value in debugger related windows such as Callstack. However, we can take advantage of DebuggerDisplay attribute to display more friendly information here. The code snippet below shows how we can decorate Customer, Order and PaymentInfo classes to show more user-friendly information in Callstack window.

C++
// Customer class
[DebuggerDisplay("Customer ID {CustomerId}")]
public class Customer
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public PaymentInfo PaymentInformation { get; set; }
}

// Order class
[DebuggerDisplay("Order Id {OrderId}")]
public class Order
{
    public int OrderId { get; set; }
    public List<Product> SelectedProducts { get; set; }
}

// PaymentInfo class
[DebuggerDisplay("CC {CreditCardNumber}")]
public class PaymentInfo
{
    public string CreditCardNumber { get; set; }
    public DateTime ExpiryDate { get; set; }
}

With these changes applied, Callstack new window looks as follows:

Image 4

Much better. Isn’t it? One thing to keep in mind is that the “Show Parameter Values” setting is unchecked by default as enabling it does add some performance cost because Debugger now needs to evaluate the values of these objects in callstack window. However, if Callstack window is not visible while “Show Parameter Values” is enabled, then there will be no performance impact.

Until next time, happy debugging!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.

He blogs regularly at http://WindowsDebugging.Wordpress.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
dmjm-h24-Apr-17 12:45
dmjm-h24-Apr-17 12:45 
Useful tip, thank you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.