how to see the values of static variables at runtime in visual studio
Asked Answered
S

5

20

The question pretty much explains what I want to do. I have several projects in c# which constitute the solution and I want to view the values of static variables at runtime in visual studio. Is there a way to do that?

Staples answered 14/2, 2013 at 6:52 Comment(2)
Does <ClassName.StaticVarName> in QuickWatch window doesn't show the value?Downstage
Actually I do not know about quickwatch window. can you please guide in this regard?Staples
A
26

Debug -> Windows -> Immediate -> type code to access your members:

[>] MyClass.MyStaticValue [ENTER]

Or put them in Watch window.

Notes:

  • You might need to add namespace too, i.e. MyNameSpace.MyClass.MyStaticValue
  • more information can be found on MSDN - Immediate Window
  • you may need to use global:: prefix if your class not found by just providing namespace (global::MyClass.MyStaticValue).
Amity answered 14/2, 2013 at 6:57 Comment(12)
sorry, but i cannot see any immediate window under view tab. I am using visual studio 2010.Staples
@VictorMukherjee there are 2 places with list of windows... I got wrong one as usual - updating.Amity
the following mesage is displayed: The expression cannot be evaluated while in run mode.Staples
@VictorMukherjee - you have to pause the application or wait until you hit a breakpoint.Reinforcement
@VictorMukherjee code needs to be stopped in debugger (breakpoint/stepping over code) to do so (same as any other debugging activity). Even if you trace during breakpoints code still breks in debugger and autocontinue - there is only limited set of things that can be done without stopping in the debugger like watching trace output and list of modules.Amity
I tried with pausing the code and evaluating as you mentioned. But then the following message is displayed: Cannot evaluate expression because a native frame is on top of the call stack.Staples
After pausing, if you get the above error, just hit Shift-F11 until you are back in managed code and the expression can be evaluated.Palinode
You might also need to include any namespace in the immediate window. This is needed in C++. Camera::WorldMatrix is not valid but Graphics::Camera::WorldMatrix is.Promote
This answer should be expanded. It's not possible to figure out how to use the immediate window from this answer. See msdn.microsoft.com/en-us/library/f177hahy.aspx for how to actually use it (it's not obvious).Bluish
@Mike - feel free to suggest an edit. I really don't know what would be useful.Amity
Also note that I had to use ? global::fully.qualified.Object.Property. Without the global, I'd get unknown identifier errors despite no conflicts.Bluish
I had to manually prepend the declaring class's name to the static variable name to see it while paused on a breakpoint in a different class when opening a Quick Watch, even though mouse hover shows its contents.Woodshed
D
7

One way is to use Immediate Window as @Alexei says.

Second way is to use QuickWatch window as below: Put a breakpoint in the class for which you want to evaluate static or any other variables/fields/properties and run the application. Then when the breakpoint is hit, right click on any variable/field/property in a class and select QuickWatch. Now, type <ClassName.StaticVarName> in the QuickWatch window textbox and press enter and you should be able to see the value as below screenshot displays:

QuickWatch

Downstage answered 14/2, 2013 at 7:13 Comment(2)
The key point is that control has to be in that class for the values of static members to be visible. Once the breakpoint is hit, an ordinary Watch works too.Chromoplast
The problem with QuickWatch is that you need to know the name of the static member first. If you want to use QuickWatch to reflect all static members of a static class then you're SOL. It's surprising that QuickWatch won't let you do that.Proto
S
4

In Visual Studio 2017, when you break the code execution you can see the values of static variables when you hover over their declarations in the source code there will be small pop-up like this:

enter image description here

  1. You can right click this pop-up and add the variable to watch window.
  2. You can click the pin to keep the variable pop-up from disappearing.
Sublunar answered 26/9, 2018 at 10:1 Comment(0)
C
0

Do you have Costura.Fody installed? I had the same issue in a project and found that it was causing static class variables to not be shown, as well as having to rebuild the project each time.

Cornu answered 1/5, 2019 at 2:22 Comment(0)
C
0

As far as I know, there's no way to show all static members of a class. Single items can be displayed in Watch or QuickWatch the Windows. my workaround is the implementation of a status method, with return type 'string', which then can be used in the Immediate window. For performance reasons, I don't use a property. If this does not concern you, you could overload the ToString() method. Then the debugger will always show its result in all relevant windows.

Cultigen answered 22/5, 2023 at 5:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.