Variable does not exist in the current context while debugging

前端 未结 5 1155
春和景丽
春和景丽 2020-12-29 21:58

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.

相关标签:
5条回答
  • 2020-12-29 22:33

    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.

    0 讨论(0)
  • 2020-12-29 22:44

    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.
       }
    
    0 讨论(0)
  • 2020-12-29 22:49

    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.

    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.

    0 讨论(0)
  • 2020-12-29 22:52

    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.

    0 讨论(0)
  • 2020-12-29 22:55

    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.

    0 讨论(0)
提交回复
热议问题