简体   繁体   English

Visual Studio C#if语句在调试中不起作用

[英]Visual Studio C# if statement not working in Debug

So I have a weird situation, where I get to an if statement while debugging, and I'm finding that even though EM=="M" , my if statement if(EM=="E") is being entered anyway... I'm also noticing that some of my code seems to be just getting skipped over when I'm stepping though the code(F11). 因此,我遇到一种奇怪的情况,即在调试时出现一条if语句,并且我发现即使EM=="M" ,我的if语句if(EM=="E")仍在输入。我还注意到,当我单步执行代码(F11)时,我的一些代码似乎只是被跳过了。

I'm using C# in VS2015 and VS2017, it's having the issue in both versions. 我在VS2015和VS2017中使用C#,两个版本都存在问题。 I was using Framework 4.5, I had switched it to 4.6.1 to build a compatible version for a different program. 我使用的是Framework 4.5,我已将其切换到4.6.1以为其他程序构建兼容版本。 But switching that back didn't change anything... 但是切换回去并没有改变任何东西...

public static string EM = "";
Database db = doc.Database;
    //db.measureunits is 0 or 1
    if (db.measureunits == 1)  // english
    {
      EM = "E";
    }
    else   // metric
    {
      EM = "M";
    }
try
{
  if (EM == "E") //If english
  {  <-- Breakpoint STOPS Here EM is equal to "M" at this point, which shouldn't allow the break point to be hit, since EM("M")!="E"
    topText.TextString = rad + "\" minimum bend radius";
  }
  if (EM == "M") //Metric
  {  <-- Breakpoint never stops Here
    topText.TextString = Convert.ToString(Math.Round((Convert.ToDouble(rad)) * 0.3048, 2)) + "\" minimum bend radius";
  }
}
catch (IOException e)
{
      // Extract some information from this exception, and then   
      // throw it to the parent method.  
      if (e.Source != null)
        System.Windows.Forms.MessageBox.Show($"IOException source: {0}", e.Source);
      throw;
}  

If anyone else is aware of this issue or know's what I may be doing wrong, help would be appreciated. 如果其他人知道此问题或知道我可能在做错什么,将不胜感激。

The following workaround might be helpful 以下变通办法可能会有所帮助

  1. Delete bin and obj folder then re-build. 删除bin和obj文件夹,然后重新构建。

  2. Restart Visual Studio OR PC 重新启动Visual Studio或PC

A few ideas. 一些想法。

  • There occures an exception so that it jumps over the code which is coming after. 发生异常,因此它会跳过后面的代码。 Press Ctrl + Alt + E and check "Common Language Runtime Exceptions". 按Ctrl + Alt + E并选中“公共语言运行时异常”。 Now it should stop at the moment an exception occures and show it to you. 现在,它应该在发生异常的那一刻停止并向您显示。
  • Your code was optimized during build. 您的代码在构建过程中进行了优化。 Try to build in debug mode or uncheck "optimize code" in the project properties under "Build". 尝试以调试模式进行构建,或者在“构建”下的项目属性中取消选中“优化代码”。
  • Delete the obj and bin folders. 删除obj和bin文件夹。 You already tried this. 您已经尝试过了。
  • There is something fishy with the PDB file. PDB文件有些混乱。 The PDB file contains information for the debugger. PDB文件包含调试器的信息。 Project properties -> build -> advanced => set "Debug Info:" to "full" (if you use debug configuration this should already be the case) and rebuild the whole solution. 项目属性->构建->高级=>将“调试信息:”设置为“完整”(如果已经使用调试配置,应该已经存在)并重建整个解决方案。

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

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