Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET

Instantiating Objects Without Stopping Your Debugging Session

4.11/5 (2 votes)
19 Aug 2012CPOL2 min read 9.6K  
How to instantiate objects without stopping your debugging session

Introduction

Sometimes during debugging of your code, you may be in part of code that is called deep within nested functions and you figure that there is a certain object that is causing the issue. In order to verify your theory, you may need to instantiate the same object with some new values. In a typical debugging session, you will change the values of that object wherever its instantiated but this will require you to stop debugging session, make code changes and then start debugging session again. However there is a quicker way of doing it also without stopping your debugging session. Let’s say you have a Point class with an overloaded + operator as shown below:

C#
class Point
{
 public int X { get; private set; }
 public int Y { get; private set; }

 public Point(int x, int y)
 {
  this.X = x;
  this.Y = y;
 }

 public static Point operator +(Point P1, Point P2)
 {
  return new Point(P1.X + P2.X, P1.Y + P2.Y);
 } 
}

Let’s say you are using this class as follows:

C#
class Program
{
 static void Main(string[] args)
 {
  Point P1 = new Point(10, 20);
  Point P2 = new Point(90, 70);
  Point P3 = P1 + P2;
 }
}

Let’s say there is something wrong in the overloaded + operator implementation. In order to figure out what the problem is, you set a breakpoint with the + operator. When this breakpoint gets hit, you figure that the problem only happens with P1 object has negative values for X and Y. However, as shown in figure below in current debugging session, you have positive values for X and Y.

Image 1

As mentioned earlier, if you want to test this function with negative values for P1 object, typically you will stop your debugging session, go back in the Main method and modify the code with the following:

C#
Point P1 = new Point(-10, -20);

Alternatively, you can achieve the same thing without leaving your current debugging session. In order to do that, first you will instantiate a Point Object in your Immediate Window.

If you haven’t used this window before, this can be brought into view by Debug -> Windows -> Immediate menu option as shown below:

Image 2

Once you get to immediate window, instantiate a Point object as shown below:

Image 3

Once this object is instantiated, you will see that this variable name Pt also becomes available in Local Variables windows. You will see that the name of variable will be prefixed with a $ symbol, which basically implies that it’s a compiler generated variable.

Image 4

Next, you assign this Pt variable to P1 in immediate window. As soon as you assign this new variable to P1, you will see that P1 variable will get updated in Locals window. In fact, values of P1.X and P1.Y will be shown in red which indicates that these values have been changed as shown below:

Image 5

This way, you were able to modify P1 object without stopping your debug session. I hope you find this tip helpful.

License

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