19

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?

Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97

5 Answers5

24

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).
T.Todua
  • 53,146
  • 19
  • 236
  • 237
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • sorry, but i cannot see any immediate window under view tab. I am using visual studio 2010. – Victor Mukherjee Feb 14 '13 at 06:59
  • @VictorMukherjee there are 2 places with list of windows... I got wrong one as usual - updating. – Alexei Levenkov Feb 14 '13 at 07:03
  • the following mesage is displayed: The expression cannot be evaluated while in run mode. – Victor Mukherjee Feb 14 '13 at 07:08
  • 1
    @VictorMukherjee - you have to pause the application or wait until you hit a breakpoint. – Damien_The_Unbeliever Feb 14 '13 at 07:14
  • @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. – Alexei Levenkov Feb 14 '13 at 07:17
  • 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. – Victor Mukherjee Feb 14 '13 at 07:23
  • 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. – thudbutt Feb 14 '13 at 07:57
  • 6
    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. – Andreas May 30 '14 at 20:47
  • This answer should be expanded. It's not possible to figure out how to use the immediate window from this answer. See https://msdn.microsoft.com/en-us/library/f177hahy.aspx for how to actually use it (it's not obvious). – Kat Jul 31 '15 at 23:36
  • @Mike - feel free to suggest an edit. I really don't know what would be useful. – Alexei Levenkov Jul 31 '15 at 23:39
  • Also note that I had to use `? global::fully.qualified.Object.Property`. Without the `global`, I'd get unknown identifier errors despite no conflicts. – Kat Jul 31 '15 at 23:41
  • 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. – Elaskanator Oct 12 '18 at 15:30
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

S2S2
  • 8,322
  • 5
  • 37
  • 65
  • 1
    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. – Ed Avis May 04 '16 at 16:57
  • 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. – Dai Dec 18 '20 at 13:53
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.
videokojot
  • 499
  • 3
  • 12
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.

Willster419
  • 114
  • 1
  • 7
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.

cskwg
  • 790
  • 6
  • 13