Breaking in code for a specific class instance using Conditional Expressions

Let me describe the problem in hand by presenting a concreate example here. Let’ say we have an Employee class as shown below. Note here that the Salary property is calculated based on certain class instance fields.

Here is how the class is consumed.

Let’s say there is an issue when calculating the Salary for Mr Problematic. There is not issue with Salary for Mr Fine. Easiest way could be to add a break-point in the getter for Salary property but then your code will stop here when Salary is computed for each Employee. Our interest is to only break when Salary is calculated for Mr Problematic. This is where combination of Make Object ID and conditional break-point (using Conditional Expression) can come in handy.

In order to accomplish that, we first need to Make Object ID for Employee class instance for Mr Problematic. This can be accomplished during a debug session once problematic instance has been created as shown below.

You can verify that object ID for this instance is created from the datatip.

Now you can simply add a Conditional breakpoint at the getter for Salary property by using a Conditional Expression of true when this == $1

At this point, you can simply hit F5. Your code will hit the breakpoint once the condition are met. You can verify that it’s the right instance by inspecting the object itself in Watch window as shown below.

One downside of this approach is that you will have to Make Object ID every time for a different debug session. Otherwise you will get following warning.

Until next, happy debugging.

Advertisements
This entry was posted in Debugging, CodeProject. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s