Debug Tips: Fetching Data stored in list and display as text in our watchlist.

Being an IT professional there would be many situations where you would need to understand the run time behavior of your application. i.e what is being stored in our variables and how is it passing

Being an IT professional there would be many situations where you would need to understand the run time behavior of your application. i.e what is being stored in our variables and how is it passing from one function to another. Chances are for performing those activities you would have debugged your application using break points and fetching the data in local, autos or watch window. I am sure that most would agree that debugging is time-consuming and at times even confusing. Add collections (lists hashtables) to this debugging and it’s literally a nightmare to study the data.

Sometimes we would want to know whether a certain value is stored in a model or not but to find it you would need to read the entire values coming from that model.

Is there any way to get around the traditional way of debugging the collections and can we apply logic such that when they are met we get the output to our local window. Well, there is we just need to use Debugger Display to get the required values.

Let’s suppose we have the below class “employee”:

namespace Models
{
    public class Employee
    {
        public int EmployeeId { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public int Salary { get; set; }
    }
}

Next you have a simple program as below:

using System;
using System.Collections.Generic;
using Models;

namespace Debugging
{
    class Program
    { 
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            List<Employee> emplist  = new List<Employee>();

            emp.EmployeeId = 1;
            emp.FirstName = "Tejaswi";
            emp.LastName = "Pandava";
            emp.Salary = 10000;
            emplist.Add(emp);

            emp.EmployeeId = 2;
            emp.FirstName = "Teja";
            emp.LastName = "swaroop";
            emp.Salary = 5000;
            emplist.Add(emp);

            emp.EmployeeId = 1;
            emp.FirstName = "Vamsi";
            emp.LastName = "srivatsan";
            emp.Salary = 20000;
            emplist.Add(emp); 

            foreach (var item in emplist)//place breakpoint here
            {
                foreach (var values in item.GetType().GetProperties())
                {
                    Console.WriteLine(values.GetValue(item,null));
                }
            }
            Console.ReadKey();
        }
    }
}

Traditional way of Debugging the collection:

Let’s debug the code and add the emplist to the watch. When an instance for emplist is created, look at it in the watch window, all you see is “Models.Employee”. We can, of course, drill in to see the individual properties, and that isn’t so bad when you’re looking at a single object, but when you look at a List<Employee> and see this, you know you’re going to spend a lot of time clicking to find the object you’re looking for.

Debugging with the help of Debugger Display:

So now lets add the DebuggerDisplay attribute to the class Employee.

namespace Models
{
    [DebuggerDisplay("EmployeeId={EmployeeId},FirstName={FirstName},LastName = {LastName},Salary={Salary}")]
    public class Employee
    {

Run the code and go to watch and see the output. So as you could see you need not to drill down to get the values for lists. Also, we came to know that we have some issue with our program, all the entries in the list are containing the same value even though we tried to add different values. So compared to the traditional method we did find the bug in this method faster.

Now let’s see how we can add some logic to this attribute and return a result only when salary is greater than or equal to 10000 else return NULL as a value.

NOTE: You need to fix the bug that causes the same values to be stored in the list.

Modify the employee class like below:

namespace Models
{
    [DebuggerDisplay("{WatchLogic,nq}")]
    public class Employee
    {
        public int EmployeeId { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public int Salary { get; set; }

        private string WatchLogic
        {
            get
            {
                if (Salary >= 10000)
                    return string.Format("EmployeeId ={0},FirstName ={1},LastName = {2},Salary ={3}", EmployeeId.ToString(), FirstName, LastName, Salary.ToString());
                else
                    return "NULL";
            }
        }
    }
}

Finally, the output as you could see the second entries value didn’t meet the condition so null is displayed against it.

Leave a Reply

Your email address will not be published. Required fields are marked *