How to use out of scope object with in Conditional Breakpoint ?

During debugging of your application, you may need to keep track of some data which are already out of scope. Say, You call a MethodB() from MethodA() and did some operation inside MethodB(), now when you returned  back to MethodA(), all the local variables inside MethodB() are out of scope. But  you want to use them in MethodA(). Now here is the trick, that you can create an ObjectID for an member variables or objects and can track the same when it’s out of scope as well, but until GC() does not collect it.  In this post, I am going to explore how we can use an Out of Scope object value to deal with Conditional Break points. To start with, first you have to understand how to use Object ID to track and out of scope object. Here is my complete article How to track an object which is Out of Scope while Debugging ? . Though I described a quick look into use of out of scope object inside conditional breakpoints on that article,  I received few query about more details on the same. So here I am going to give some more details.

Let’s consider we have below code block

class Program
{
///
/// Mains the specified args.
///
/// The args.
static void Main(string[] args)
{
//Call Method A
MethodA();
}</pre>
///
/// Methods the A.
///
private static void MethodA()
{
//Do Some Operation
//Call Method B
MethodB();
// Do Some Operation
// Create a local List object of Students
List studs = new List{
new Student{Roll=1,Name="Abhik"},
new Student{Roll=2, Name="Abhijit" },
new Student { Roll = 3, Name = "Abhishek" },
new Student { Roll = 4, Name = "Rahul" },
new Student { Roll = 5, Name = "Ritaban" },
};
// Print them in Console
foreach (Student stud in studs)
{
Console.Write(stud.ToString());
}
}
///
/// Methods the B.
///
private static void MethodB()
{
// Createa a local Object of Student
Student stud = new Student { Roll = 1, Name = "Abhijit" };
}
}

///
/// Student Class
///
class Student
{
public int Roll { get; set; }
public string Name { get; set; }
public override string ToString()
{
return "Roll : " + this.Roll.ToString() + " Name : " + this.Name + "\n";
}
}

Well that was the simple enough code. Objective is to see how things works with Out of scope object.  Now, come to the exact point, You want to check some data value before print the data in console in MethodA() and you want to make sure that, Your breakpoint will only stop if the Student Name is same as object create in MethodB(). So, you have to put a conditional breakpoint over here. But does conditional breakpoint is sufficient to stop your breakpoint ? well yes, but you have to deal with some object which is not in current scope. 

So, first of all you need to put a breakpoint at MethodB() where you are creating the Student object. and you need to create an object ID for the same. ( From Watch Window > Right click on Object > Make Object ID)

image   

From the above image, you can see how I have created an object ID ( 1#)  from the object “stud” in MethodB().

Now, the next step is, using the Object ID in Conditional Breakpoint at MethodA().

image

That’s all, Now, your breakpoint at “Console.WriteLine” will only hit when the local student object name will be same with the stud object at methodB() ( 1#) name.

image

From above image you can see, break point stopped when object ID (1#)’s name same as current object’s name value.  If you have multiple name that matched with object ID’s value, your breakpoint will stop multiple time.

Note : After create the object ID if you stop and restart the debugging again, if you have to create the Object ID once again. other wise you will get error “Object ID not Found” also when GC() collect the object.

image

Summary : In this post, I have explained how we can use an out of scope object with in an Conditional breakpoint. This is really helpful when you want to debug with some value which are not inb scope.

Hope this will help you !

Image Ref :

One thought on “How to use out of scope object with in Conditional Breakpoint ?

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