Click here to Skip to main content
15,896,063 members
Articles / .NET
Tip/Trick

Modifying Variable Values without Leaving Your Debugging Session

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
18 Aug 2012CPOL2 min read 11.8K   2   3
How to modify variable values while remaining in your debugging session
In this tip, you will see a couple of ways of modifying the value of a variable without stopping the debugging session.

Introduction

Sometimes, during debugging of your code, you have to modify value for a particular variable. In a typical debugging session, you will first stop that debugging, make code changes and then start debugging again. But there are few other quick ways of modifying variable values without stopping and re-starting a 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 we want to modify the value of P1.X to something else in the overloaded + operator. I will show you couple of ways of doing it without stopping the debugging session. First is from Locals window. You can not only inspect local variables through this window but you can also modify values for these variables. In order to modify the value of variable, you have to right click and then select Edit Value option as shown below.

After selecting this menu option, we modify the value of P1.X to 100. Once this change is made, the updated value will be shown in red within Locals as shown below.

Another way of changing variable values is by writing code in Intermediate Window. As shown in the figure below, we have updated the value of P1.X in Intermediate Window by writing P1.X = 500; in Intermediate window. This change is reflected in Locals window as well.

One more way of doing it is using Datatips. If you hover your mouse the P1.X in the code, a small window will appear showing the value of this variable (as shown in the figure below).  

At this point, you can just double click on the value 10 and modify to whatever value you need. As shown in figure below, I changed the value to 900. Once this value is modified, it will be displayed in red within the data tip and in Locals window.

I hope you find this tip helpful.

History

  • 17th August, 2012: First version
  • 18th August, 2012: Second Version: Update to include DataTips based on feedback from varun_minpal

License

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


Written By
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

Comments and Discussions

 
GeneralMy vote of 2 Pin
Rahul.KumarSharma18-Aug-12 21:02
Rahul.KumarSharma18-Aug-12 21:02 
SuggestionAnother shortcut for the same thing. Pin
varun_manipal18-Aug-12 5:38
varun_manipal18-Aug-12 5:38 
GeneralRe: Another shortcut for the same thing. Pin
Kamran Bilgrami18-Aug-12 8:25
Kamran Bilgrami18-Aug-12 8:25 

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.