简体   繁体   English

调试时,当前上下文中不存在变量

[英]Variable does not exist in the current context while debugging

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. 我打破了所有这三点,但我无法让Visual Studio 2012向我展示价值观。 Only result will show up on the locals window, there is no variable called Radian or Degree. 只有结果会显示在locals窗口中,没有名为Radian或Degree的变量。

If I add a watch for Radian variable for example, I get this message with a red cross icon: 例如,如果我为Radian变量添加一个监视器,我会收到带有红色十字图标的消息:

Radian - The name 'Radian' does not exist in the current context Radian - 当前上下文中不存在名称'Radian'

It's possible the local variables have been optimised away by the JIT compiler. JIT编译器可能已经优化了局部变量。 Since you're using Visual Studio you might be able to switch the configuration to Debug and rebuild. 由于您使用的是Visual Studio,因此可以将配置切换为Debug并重建。

If not, you can configure the JIT compiler to disable optimisations and generate tracking information - see here on how to set the configuration . 如果没有,您可以配置JIT编译器以禁用优化并生成跟踪信息 - 请参阅此处有关如何设置配置的信息 This should allow you to see local variable when you attach the debugger to the process. 这应该允许您在将调试器附加到进程时查看局部变量。

I've encountered another scenario in VS2012 that causes variables to "disappear" while in debug mode: 我在VS2012中遇到了另一种情况,导致变量在调试模式下“消失”:

make sure you don't have this: 确保你没有这个:

if(false)
   {
   .
   }
else
   {
   //Code here will be optimized and variables will not be available.
   }

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. 将您的解决方案配置更改为Debug(任何CPU),您将能够在即时窗口中看到变量值。

I changed my Solution Configuration to Debug (Any CPU) but that wasn't the problem. 我将我的解决方案配置更改为调试(任何CPU),但这不是问题。 After upgrading to Visual Studio 2017 I thought I was in Debug but there was one more simple (but important) step. 升级到Visual Studio 2017后,我以为我在调试中,但还有一个更简单(但很重要)的步骤。 (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. 从那里重新运行允许我在调试时查看所有变量。

I had a .NET Standard project, and what worked for me was to go: 我有一个.NET Standard项目,对我有用的是:

  • 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. 将其更改为“Full”允许我再次在Watch和Immediate窗口中查看变量。

Not sure if it it's relevant, but I had recently converted the project from PCL to .NET Standard. 不确定它是否相关,但我最近将项目从PCL转换为.NET Standard。 Maybe something got lost in translation. 也许在翻译中丢失了一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM