views:

549

answers:

5

I am curious to know if there is a way to edit code in C# VS 2008 right when it has hit a breakpoint and I am walking thru the code... can I modify the code (such as the value in a variable or if my stepthrough line is about to hit an if statement ... can I modify the if statement....etc)? So far, I have to stop running VS, modify the code, then hit F5 and wait till the breakpoint is hit again.

When the breakpoint hits, and I am walking thru the code, and I attempt to edit the code, I get a message "Changes are not allowed when the debugger has been attached to an already running process of the code being debugged was optimized at build or run time."

+4  A: 

Yes, you can.
This is called Edit and Continue.
Note that it has some limitations.

EDIT: You need to switch to a debug build.

SLaks
How do I do "Edit and Continue" ?
user279521
Break into the debugger (either hit a breakpoint or press the pause button) and *Edit* the code then *Continue* with the Play button or F5 (or F10 or F11 depending what you're doing)
Paolo
+1  A: 

There are only a few reasons I know of why Edit+Continue would be disabled in the Debug build. First and foremost is a 64-bit operating system, E+C only works for 32-bit code. Fix that with Project + Properties, Build tab, Platform Target = x86.

It is also an option that might have been turned off. Tools + Options, Debugging, Edit and Continue, Enable checkbox.

If this doesn't help, tell us a bit more about the kind of code you're debugging, the project template you selected when you started the project, how you got the debugger to break and a stack trace copied from the Call Stack window.

Hans Passant
user279521
I am debugging C# code in the code behind. The project template as WebForm.aspx. I put a breakpoint to get the debugger to break. How can I get the stack trace copy?
user279521
@user, read this: http://whyiamright.wordpress.com/2007/12/20/aspnet-edit-and-continue-in-visual-studio-2005/
Hans Passant
user279521
A: 

In response to this question:

can I modify the code (such as the value in a variable or if my stepthrough line is about to hit an if statement ... can I modify the if statement....etc)?

You cannot pop a new value into a variable directly, but what you can do is this:

  1. Set a breakpoint
  2. When that breakpoint is hit, click on the arrow in the left margin and drag it up to a previous line
  3. Now you can add code to change the circumstances (for example, you can set a variable to a new value, add/remove items from a collection, etc.)

See the other answers about enabling Edit & Continue -- in particular, make sure you're in Debug mode.

Dan Tao
A: 

To solve this problem I did the following...

BUILD > CONFIGURATION MANAGER

Active solution configuration: DEBUG project context configuration: DEBUG

then TOOLS > OPTIONS > DEBUGGING > EDIT & CONTINUE make sure edit & continue is selected

then BUILD > CLEAN SOLUTION then BUILD > REBUILD SOLUTION

Then start debug, then pause, then your code should be editable

Ian
+1  A: 

To modify the value of a variable or set a property while in break mode go to the Immediate window, set the new value, and hit return e.g.

strValue = "newValue"
Calendar1.Enabled = true

To retrieve a value you can just print it to the window e.e.

?strValue
?Calendar1.Enabled
Ciaran Bruen