30

I inserted two temp variables and want to see their values, but I can't. I could solve it by placing it somewhere else, but I'm interested why this behaviour exists.

   public float Value
    {
        get
        {
            float result = Memory.ReadFloat(Address);

            double Radian = Math.Round(result, 2); // **BREAK POINT HERE**
            double Degree = Math.Round(Math.Round((double)(result * 180 / Math.PI)), 2); // **BREAK POINT HERE**

            return result; // **BREAK POINT HERE**
        }
    }

I break on all three points, but I can't get Visual Studio 2012 to show me the values. Only result will show up on the locals window, there is no variable called Radian or Degree.

If I add a watch for Radian variable for example, I get this message with a red cross icon:

Radian - The name 'Radian' does not exist in the current context

random
  • 9,774
  • 10
  • 66
  • 83
Dominik Antal
  • 3,281
  • 4
  • 34
  • 50

5 Answers5

68

It's possible the local variables have been optimised away by the JIT compiler. Since you're using Visual Studio you might be able to switch the configuration to Debug and rebuild.

If not, you can configure the JIT compiler to disable optimisations and generate tracking information - see here on how to set the configuration. This should allow you to see local variable when you attach the debugger to the process.

Lee
  • 142,018
  • 20
  • 234
  • 287
21

I've encountered another scenario in VS2012 that causes variables to "disappear" while in debug mode:

make sure you don't have this:

if(false)
   {
   .
   }
else
   {
   //Code here will be optimized and variables will not be available.
   }
ishayle
  • 355
  • 4
  • 11
  • Please, try to read this http://stackoverflow.com/about, to get more understanding about questions/answers here on SO. Your contribution is not answering the question. It is more a comment, which you can add once you'll increase your reputation: http://stackoverflow.com/faq#reputation – Radim Köhler Jan 22 '14 at 07:49
  • 12
    @RadimKöhler, you're wrong. The answer he put here saved my day, I was crazy to solve this problem and it solve simples as that. I've added a if(false) in my code to avoid a long running block and test another point, my mistake was the same as described by ishayle – Diego Mendes Apr 19 '14 at 07:22
  • So this answer helped me too, but the code optimized was not inside the else structure. Instead of commenting out code I sometimes add if(1==2) above an if else. Turned out that variables below this were not available to the debugger. – CLRBTH May 11 '14 at 09:37
  • I had the same problem here and this solves the "Variable does not exist in the current context"-Bug for mee, too (Visual Studio 2010 SP1)! In the past few hours I only found suggestions like "build with Debug and don't use optimization". I did that! But I never thought a if(false) would cause Visual Studio 2010 to fall into this behavior. Clearly a bug in VS! To say it again: If you don't see Variable content on mouse hover or under Watch/Locals, search in the method or class for an if(false) or something that >might< be optiimized by the compiler (even in Debug mode) and delete it. – Gerrit Mar 25 '15 at 10:18
  • 1
    I would upvote this answer +20 if I could. This problem has plagued me on and off for months in Visual Studio 2010 and I could never figure out what triggered it. I've done many online searches on this and this is the only answer explaining this nuance I've come across. It was so annoying to have to lift local variables to higher scopes just to see them in the debugger! Thank you once again. – Special Sauce Jan 08 '16 at 08:49
15

If you are trying to debug in a release build (release mode instead of debug mode), you'll get this error. Change your solution configuration to Debug (Any CPU) and you'll be able to see variable values in the immediate window.

Chewdoggie
  • 379
  • 3
  • 9
  • What happened to me was that all project under debug in was set to release so all code was optimized... I didn't realize it until I saw your answer and it clears up a lot of questions I've had lately :) – Asken Oct 24 '16 at 03:15
2

I had a .NET Standard project, and what worked for me was to go:

  • Project properties.
  • Build tab.
  • Advanced button (all the way at the bottom-right).
  • Change the "Debugging information" option to full.
  • List item

It had been set to "Portable". Changing it to "Full" allowed me to see variables in Watch and Immediate windows again.

Not sure if it it's relevant, but I had recently converted the project from PCL to .NET Standard. Maybe something got lost in translation.

NightShovel
  • 3,032
  • 1
  • 31
  • 35
0

I changed my Solution Configuration to Debug (Any CPU) but that wasn’t the problem. After upgrading to Visual Studio 2017 I thought I was in Debug but there was one more simple (but important) step. (And it’s probably obvious to most, but I missed it.) Where “Solutions Configurations” is set to “Debug” I had to ALSO click the down arrow next to “Debug” and select “Configuration Manager…” and then in that corresponding popup window “Configuration” was still set to “Release” – I had to change that to “Debug” and click the “Close” button. Rerunning from there allowed me to view all variables while debugging.

Crazy Cat
  • 1,332
  • 15
  • 19