14

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."

5
  • I am using C#. I tried it but I get a message saying "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."
    – user279521
    May 6, 2010 at 13:13
  • Yes, the switch to debug build. Well, before I hit F5, it is in debug build. After I hit F5 and the breakpoint hits, the "build" dropdown is grayed out.
    – user279521
    May 6, 2010 at 13:21
  • You don't need to (and cannot) rebuild using E&C. Your changes will be applied automatically.
    – SLaks
    May 6, 2010 at 14:06
  • What do you mean by "You don't need to (and cannot) rebuild using E&C. Your changes will be applied automatically" ?
    – user279521
    May 6, 2010 at 14:10
  • He means that if you just continue executing (either by stepping or pressing f5) then the changes you made will be applied. Note that this only works in 32 bit builds, not 64 bit. Oct 4, 2010 at 18:07

7 Answers 7

6

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.

4
  • Target platform was "Any CPU". I changed to x86, E&C still doesn't work. The Tools + Options, debugging. E&C checkbox is enabled.
    – user279521
    May 6, 2010 at 14:05
  • 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
    May 6, 2010 at 14:09
  • 2
    @user, read this: whyiamright.wordpress.com/2007/12/20/… May 6, 2010 at 14:47
  • 1
    WOW !! The second setting in the "Web" tab did the trick. E&C was disabled. Your original response said Build tab. Eitherway, THANKS HANS !!!
    – user279521
    May 6, 2010 at 14:53
4

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

EDIT: You need to switch to a debug build.

2
  • How do I do "Edit and Continue" ?
    – user279521
    May 6, 2010 at 12:37
  • 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
    May 6, 2010 at 12:39
1

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
1
  1. Stop running your app.
  2. Go to Tools > Options > Debugging > Edit and Continue
  3. Disable “Enable Edit and Continue”.
0

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.

0

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

0

Below answer worked for me :

http://whyiamright.wordpress.com/2007/12/20/aspnet-edit-and-continue-in-visual-studio-2005/

The second point says - project-properties-web-> enable edit and continue.

thats it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.