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

时间:2018-03-29 14:35:19

标签: c# visual-studio if-statement visual-studio-debugging

所以我有一个奇怪的情况,我在调试时得到一个if语句,我发现即使EM=="M",我的if语句if(EM=="E")仍然被输入..我还注意到,当我踩过代码(F11)时,我的一些代码似乎只是被忽略了。

我在VS2015和VS2017中使用C#,它在两个版本中都有问题。我使用的是Framework 4.5,我已将其切换到4.6.1,以便为不同的程序构建兼容版本。但转回去并没有改变任何事情......

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;
}  

如果其他人知道这个问题或者知道我可能做错了什么,那么将不胜感激。

2 个答案:

答案 0 :(得分:0)

以下解决方法可能会有所帮助

  1. 删除bin和obj文件夹,然后重新构建。

  2. 重新启动Visual Studio OR PC

答案 1 :(得分:0)

一些想法。

  • 发生异常,以便跳过后面的代码。按Ctrl + Alt + E并选中“Common Language Runtime Exceptions”。现在它应该在发生异常时停止并显示给你。
  • 您的代码在构建期间进行了优化。尝试在“构建”下的项目属性中构建调试模式或取消选中“优化代码”。
  • 删除obj和bin文件夹。你已经试过了。
  • PDB文件有些可疑。 PDB文件包含调试器的信息。项目属性 - &gt;构建 - &gt; advanced =&gt;将“Debug Info:”设置为“full”(如果使用调试配置,则应该已经是这种情况)并重建整个解决方案。