Click here to Skip to main content
13,507,742 members
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 3 Jun 2012

Conditional Breakpoint using Make Object Id feature

, 3 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Effective way of debugging by taking advantage of some not well-so-known features of Visual Studio


I have blogged about using Visual Studio "Make Object Id" feature here for inspecting out of scope objects. In this tip, I will demonstrate another (hopefully) useful scenario where this feature could be utilized.


Let’s say you have hundreds of managed objects on the heap and you need to debug certain part of the code only when a particular instance out of those objects is referred in that piece of code. Typically you will put a breakpoint in the method where you want to break. However, if this method is invoked for all those hunderds of managed object then this breakpoint will hit so many times, you may feel tired of waiting for your lucky time when method is been hit for instance that you are interested in.


The sample code I am going to show here is very simple but its good enough to give you a good idea of this use case.

Let's say, we have an Employee class. Then we have a method LoadEmployees where we are loading 1000 employees and adding in a list. Please note that I have used the  Member.GeneratePassword API just for generating some temporary strings to be used for last and first name of the employee. Then for each of Employee in that list, we are calling several methods like ProcessSalary, ProcessRetiremendFund, etc., that are doing some processing related to various aspects of benefits. For simplicity, I am just printing some text message with employee's id in each of these methods.

class Employee
    public int    Id        { get; set; }
    public string LastName  { get; set; }
    public string FirstName { get; set; }

    public Employee(int id, string last, string first)
        this.Id         = id;
        this.LastName   = last;
        this.FirstName  = first;

class Program
    static void Main(string[] args)
        List<employee> <Employees> = new List<Employees><employee>();

        foreach(Employee emp in Employees)





    static void LoadEmployees(List<employee> Employees)
        for (int i = 0; i < 1000; i++)
            Employee emp = new Employee(i, Membership.GeneratePassword(6, 0), 
                               Membership.GeneratePassword(6, 0));
    static void ProcessSalary(Employee emp)
        Console.WriteLine("Salary Processing for employee with id = " + emp.Id);

    static void ProcessRetirementFund(Employee emp)
        Console.WriteLine("Retirement funds Processing for employee with id = " + emp.Id);

    static void ProcessMedicalBenefit(Employee emp)
        Console.WriteLine("Medical Benefits Processing for employee with id = " + emp.Id);

    static void ProcessDentalBenefit(Employee emp)
        Console.WriteLine("Dental Benefits Processing for employee with id = " + emp.Id);

    static void ProcessLifeInsurance(Employee emp)
        Console.WriteLine("LifeInsurance Processing for employee with id = " + emp.Id);

Let's say something is happening wrong in processing related to an employee with id 775. Since processing is been implemented in several methods, we are not sure which method is faulty and as a result we need to debug all the ProcessXXX methods. However, since these ProcessXXX methods are called for each of 1000 employees in the list, it will be not efficient way of debugging by breaking in each of these methods for every single employee. Rather we can simply add a conditional breakpoint for employee with id 775 as follows.

This was simple to do because the only criteria we have match is on that object's Id field. However, this technique becomes tedious when you have to match on multiple fields of object. In that case, you can use another technique that involves two steps. First step is to use "Make Object ID" feature on the class instance. This can be done by hovering the mouse over on the emp object of interest ( meaning when its id = 775). Once the data-tip shows up, right click on the variable name and you will get an option for "Make Object ID" as shown in figure below.

Once you select "Make Object ID" option from context menu, this particular object will be available to be referred as 1#. If you used this option for any other variables then you will see the number 1 ( in 1#) getting incremented. At this point you will also notice that 1# will be added in object value under Locals window as shown below.

Now that this variable is available to be referred as 1#, we can use this short-cut in our conditional breakpoints. All the methods where you want to break, you can add a conditional breakpoint using this shortcut as follows rather referencing all the fields that you want to match on.

I hope you will find this tip helpful.


  • First version added on June 03 '2012.


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


About the Author

Kamran Bilgrami
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

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180417.1 | Last Updated 3 Jun 2012
Article Copyright 2012 by Kamran Bilgrami
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid