Click here to Skip to main content
13,147,591 members (28,869 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 17 Aug 2012

Instantiating Objects Without Stopping Your Debugging Session

, 19 Aug 2012
Rate this:
Please Sign up or sign in to vote.
How to instantiate objects without stopping your debugging session


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:

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:

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.

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:

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:

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

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.

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:

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


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

QuestionNice Work Pin
Richard Andrew x6418-Aug-12 13:45
memberRichard Andrew x6418-Aug-12 13:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 19 Aug 2012
Article Copyright 2012 by Kamran Bilgrami
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid