Amokrane Chentir
Amokrane Chentir

Reputation: 30405

Is it possible to change the value of a variable during execution time?

I want to know if there is any way I can change the value of a variable during execution time, using Visual Studio 2008? I know I can do this on Eclipse (put a breakpoint, add a watcher and edit whatever variable value I want) but I didn't find this feature on VS yet?

Any idea?

Thanks.

Upvotes: 48

Views: 91387

Answers (7)

T.S.
T.S.

Reputation: 19404

When the code is stopped, there are many ways to change the value as answers here suggest. But there is also a way to change it without code stop or code modification.

For example, I needed to create a condition that would start throwing errors or write log messages. I did not want to modify the code. What I did, I inserted a Tracepoint and assigned an invalid value to a variable, which later was throwing an error. And for testing multi-threaded code, when you don't want to stop, this is perfect. The only downside, the value is written to the Output window.

enter image description here

Upvotes: 1

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102448

Use the Immediate Window.

Use the Immediate window to debug and evaluate expressions, execute statements, and print variable values. The Immediate window evaluates expressions by building and using the currently selected project.

Upvotes: 11

Adam Houldsworth
Adam Houldsworth

Reputation: 64547

In VS if you hover your mouse over this variable, you'll notice it displays it's value in a tooltip. You can click into this tooltip and edit it manually - though you need to provide a value of the correct format for the data type. Strings also need quotes "".

There is likely a way to do this via some of the other debugging windows, but I don't know of any. I'll leave those answers to someone else.

Upvotes: 60

Chinjoo
Chinjoo

Reputation: 2832

You can do this as follows:
1. Put a breakpoint just after the line of code where you want to update the varaible value.
2. Run the application till the breakpoint is hit.
3. Just hover over the variable you want to edit and the varable name and value will be shown.
4. Click on the Value field and edit it, press enter.

Mission accomplished...

Upvotes: 4

Ram
Ram

Reputation: 11644

You can do it using one of the following ways

  • User Immediate window
  • Using quickwatch window

Both allow user to change the variables value.

Upvotes: 5

Mitch Wheat
Mitch Wheat

Reputation: 300797

Yes. There are several Ways.

Double-click the variables value in the Watch, Local or Auto's window. You can also do this from the immediate window.

Upvotes: 5

Oded
Oded

Reputation: 499382

In VS, there is the command/immediate window (when debugging, under the Debug -> Windows menu) which allows you to change variable values.

A more visual way is the Variable window.

Upvotes: 24

Related Questions