Click here to Skip to main content
Click here to Skip to main content

Instantiating Objects Without Stopping Your Debugging Session

, 19 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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.

License

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

Share

About the Author

Kamran Bilgrami
Architect
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 http://WindowsDebugging.Wordpress.com
Follow on   Twitter

Comments and Discussions

 
QuestionNice Work PinmemberRichard Andrew x6418-Aug-12 14:45 

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

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

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