Editing code in Visual Studio 2008 in debug mode - c#

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

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.

Yes, you can.
This is called Edit and Continue.
Note that it has some limitations.
EDIT: You need to switch to a debug build.

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

Stop running your app.
Go to Tools > Options > Debugging > Edit and Continue
Disable “Enable Edit and Continue”.

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:
Set a breakpoint
When that breakpoint is hit, click on the arrow in the left margin and drag it up to a previous line
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.

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

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.

Related

Button "Step Back Over" always disabled

While debugging in Visual Studio v16.3.2 I want to back on the previous breakpoint and I am trying to achieve it via Step Back Over button. Why this button always disabled?
The reason could be that it's disabled in your IDE.
Try
Tools > Options > IntelliTrace > General
and in there make sure that “IntelliTrace events and snapshots” is checked.
This might or might not require a restart of the IDE.
I am not sure if you are using correct configuration but link can help you setting.
Step Back Debugging
Other possible reason could be, is step back allowed on variable assignments?

Visual Studio 2012/C# breaks code execution, no breakpoint set

I'm having a problem in Visual Studio 2012/C# which is driving me crazy. I have a particular line of code in a C# file with NO breakpoint set. Every time this line should be executed the debugger interrupts the execution of my code like a breakpoint was set. The only difference i noticed is the arrow on the left hand side, indicating the current step. Usually this arrow is yellow (when a breakpoint is set). In my case it is grey.
I have been searching for a solution but did not find anything useful to this. I tried deleting all breakpoints, build project, rebuild project, clean project and it still appears.
Does anyone have the same problems and found a solution this?
normal arrow:
arrow in my case:
This line of code is currently executing, it calls something else, e.g. native code or .NET internal code and an exception happens there. You can see both arrows in the call stack window:
Perhaps you need to turn on "Show external code".
If you can't see the exception dialog, usually you can show it like this:

Visual Studio debugger - run until next user prompt

I'm in a Visual Studio debugging session, debugging a WinForms app, but I think this question applies to an ASP.Net code-behind debug session also. Let's say that I have lots of breakpoints set and I want to test a condition that requires running a setup test case first, then I want to run another test case that exercises the code again. The first time, I have to hit the Continue (F9) button 5 times to progress through the code breakpoints and finally arrive at the point where I'm prompted for input again. Now I want to input some data and I now want to carefully step through all the breakpoints.
Is there some way I can quickly push a button and tell the debugger to skip all those breakpoints during the setup test case entry and just progress to the next data input field? I know about the run to cursor capablilty, but that doesn't seem any easier than clicking thru all of the breakpoints.
I know this may sound trivial, but I find myself in this situation all the time.
I just want a super-continue button.
You can open the Breakpoints window (Debug -> Windows -> Breakpoints) and then you can select multiple breakpoints and disable or enable them as you go:
The best thing i can think of would be to create a condition on the breakpoint to only break when the data is set up.
E.g.
Or if all else fails, set a BreakPoint Hit Count, if you know it'll always be the 6th break that is relevant. E.g.

How to debug a C# command-line program

I'm trying to build a command-line tool in C# with VS2010.
My question is: how do I debug this, like I would a winforms.
With winforms, I can step through the code, see the values at each individual step, etc...
Here however, the program only responds when I talk to it from the command line. I can't start a debug session, since that will first fully start the program. And due to the nature of the program, it will shut itself because there were no command-line arguments.
So no debugging can occur.
What do I need to do here?
EDIT: an example, since someone made a comment that makes me feel this explanation is needed.
C:\Path\To\File\file.exe -help
That is an example of how this program is adressed. The command, -help, is given in the same line that the program is started. In other words, I cannot first start the program, and then give it a command while it's running... because it won't be running anymore. It'll start, see that it had no arguments on startup, and because of that, shut down. That's how a command-line tool works.
Activate
Process possible arguments
Output results
Shut down automatically
It is not something that keeps running till you click the little x in the top right corner.
In the Project properties, under Debug, you can enter any Command Line Arguments you would like, and then run the app with F5, the debugger will be attached automatically.
You could add a call to Debugger.Launch to your startup code. Then you can compile, and start your app from the command line. You'll get a prompt asking you which debugger you want to attach (typically this will be a list of the different versions of Visual Studio you have installed), and away you go.
(But really, setting command-line parameters in Project properties > Debug tab is the better way to go most of the time. If that's not working for you, you should figure out why.)
You can use Visual Studio to attach a debugger to the command line application, once it is under way with the correct arguments. I'm not sure if your application will terminate quickly or give you any opportunity to attach the debugger, but if it will, this should work.
I'm using VS2008 but I'll bet the process is similar in 2010:
In VS, go to Tools and click Attach to Process
Choose your application from the list and press Attach
Now VS should be able to finagle its way into your application and break on an error.
You just need to add a breakpoint to the first line of the main function (you can do this by clicking on the line in the Visual Studio editor and hitting F9) and hit F5 to start a debug session.
Either add a breakpoint to the opening { of Main, or step into the program (Debug menu). At that point set a watch on the parameter for main (the command line arguments) by selecting it and either right-click/Add Watch or drag the parameter to to watch window if it is already open. Double-click the Value column in the Watch window and set it to whatever you want it to be.
Note: the value added must be valid code -- that is, to add a -help to the string[] you would have to type new string[] {"-help"} or new [] {"-help"} depending on the version you are using.
This has the advantage over setting the parameter in the Debug tab of the Properties window by allowing different parameters for each run without having to return to the Properties window.
When you have command line arguments then you need to follow different route to debug. Go to project and then select properties. There you will see debug section and then give required parameters. So that it will take care of passing parameters to the program. If you have multiple parameters then separate them with space. After that if you debug it will hit the break point directly. For more info check here how to debug c# through command line arguments in visual studio

Your step-into request resulted in an automatic step-over of a property or operator

I am getting the following error message while running .net 3.5 applciation
Your step-into request resulted in an automatic step-over of a
property or operator.
This behavior can be overridden in the context menu for the line being
executed by choosing 'Step Into Specific' or by unchecking the option
'Step over properties and operators'.
Do you want continue being notified when an automatic step-over
happens?
What does this error message mean?
VS2017 and VS2019:
Tools > Options > Debugging > Uncheck "Step over properties and operators > (Managed only)".
It is not an error message as such. The IDE is telling you that tracing for some of your code is being skipped during debugging due to the current settings. If you want to be able to trace into the code, change the settings as described in the message.
You can change this behavior by going to: Tools -> Option -> Debugging.
The setting for this in VS2010 is under: Tools -> Option -> Debugging (near the middle)
To be more specific: the option to enable in Visual Studio 2010 is:
Tools->Options->Debugging->General->Enable property evaluation and other implicit function calls
As answered by other people this is an informational message from Visual Studio telling you that it could have stepped into a line of code but rather stepped over it due to current dev environment settings.
There are three ways to change this behaviour in VS2012:
Change the settings: Tools->Options->Debugging->General->Step over properties and operators
OR
Right click on the line of code to get the context menu. Then untick: Step over properties and operators
OR
Select 'Step into Specific' in the right click context menu which will ask you which specific function you would like to step into. It will list all the properties/functions involved in the current source line.
In Visual Studio 2013: right click on the line that caused the message to pop-up.
This will bring up the context menu.
Uncheck the option: Step over properties and operators.
Other posts have the correct answer, which state that you can change the option in Tools > Options > Debugging > Step over properties and operators (Managed only) in Visual Studio. I wanted to add an image from the Options dialog for those who are visual. Uncheck the property if you want to perform Step Into (F11) without automatic Step Over (F10).
The reason that we get this prompt is: that we may have created properties or operators in our classes, and when, during debugging, we reach that line of code, it is stepped over (like the effect of F10 ) instead of stepping into ( the actual effect of F11 )
e.g., this line of code,
having pressed F11 here, resulted into effect of pressing F10
So Visual Studio notifies us..and gives this beautiful, well illustrated message, which I could only understand when I read the following blogpost
Credits: AutoStepOver a blog post
The other answers are suffient for turning the feature on or off. Lacking is the insight as to WHY one would want to do one or the other.
For beginning C# programmers their property method is a simple {get,set}. Since this code is not worth viewing we check the box "Step over properties...".
However, when your property settings become more interesting, you may want to step into the property method to ensure it's behavior is correct. Once you tell the IDE "Don't bother me any more", then later when your "step into" fails for a complex property method, now it's your fault.
My recommendation would be either to remember how to switch the option on and off for future debugging sessions or uncheck the "Step over..." setting and learn to toggle between F11 and F10 as appropriate.
Another option is never to use "step into". Just set a breakpoint in the method/property/operator you want to debug and click the step (F10). This way you step into only the methods you are debugging.

Categories