Matt Cashatt
Matt Cashatt

Reputation: 24198

Visual Studio--Alter Variable While Debugging

Is it possible to alter a variable while debugging?

Say for example I have this code:

    string x = "foo"; //would actually be a passed-in variable in the real world.

    var y = "X equals " + x;    

    ==>[BREAKPOINT] return x; 

Is there a way to manually enter or otherwise change the value of "x" when I hit the break point? Also , is it possible to "step back" in the code in the same way that you can press F11 and step through it?

Upvotes: 1

Views: 81

Answers (1)

Simon C
Simon C

Reputation: 9508

Yes, there are 2 ways to change a variable:

  • Use the Immediate window. Simply typing x = "a new value" will change it. Also, if you want to check the value of x use ?x
  • You can hover over the variable, and when the value displays in the quickwatch pop-up thing, just click on it an manually change it. (You can also add the variable to watch, or select quickwatch to change it).

If you would like to step to a different instruction, there are 2 ways to do that too:

  • You can right-click on the line you want to go to and select 'Set next statement'. This even works if you have hit an exception while debugging
  • You can drag the yellow arrow that indicates the current instruction to wherever you need it

Upvotes: 3

Related Questions