if语句在C#VS 2012中没有结果

时间:2012-09-04 12:35:43

标签: c# 64-bit visual-studio-2012

我在VS 2012中有这样的代码:

private void Form1_Load(object sender, EventArgs e)
    {
        if (Properties.Settings.Default["Database"] != null)
        {
            MessageBox.Show("We landed on spot 1");
        }
        else
        {
            MessageBox.Show("We landed on spot 2");
        }
    }

我很确定我搞砸了条件语法,但我希望其中一个会发生:

  1. 编译器警告错误/项目无法运行。
  2. 显示第一条消息
  3. 显示第二条消息。
  4. 但这些都不会发生。我一直盯着这一个小时,我能找到的资源非常渺茫。 如果有经验的人可以解释我这里发生了什么?

    修改 感谢JMK's link我发现这基本上是在Windows x64下VS调试器中弹出的wontfix错误。如果应用程序在调试器之外运行,则会触发错误。

3 个答案:

答案 0 :(得分:3)

它默默地出错。

    try
    {
        if (Properties.Settings.Default["Database"] != null)
        {
            MessageBox.Show("We landed on spot 1");
        }
        else
        {
            MessageBox.Show("We landed on spot 2");
        }
    }
    catch (Exception ee)
    {
        MessageBox.Show(ee.Message);
    }

回来时“找不到设置属性'数据库'”

答案 1 :(得分:0)

尝试在Properties

之前添加项目命名空间
if (WindowsFormsApplication2.Properties.Settings.Default.Database != null)

答案 2 :(得分:0)

可靠的是抛出异常而调试器没有注意到。 对于64位Windows版本上的Windows窗体项目会发生这种情况(并且不是特定于.NET的行为,而是针对Windows的行为)。

此处有更多详情:Visual Studio does not break at exceptions in Form_Load Event

尝试按STRG + ALT + E并选中“公共语言运行时例外”复选框“Thrown”。 现在,调试器将在Form_Load()

中的任何异常中断

因为我知道我的解决方法是完全避免使用Load事件。

我的大多数表单都是对话框,因此我隐藏了ShowDialog()方法并调用了Init()函数。

public class Form1
{

    public new DialogResult ShowDialog()
    {
        Init();
        return base.ShowDialog();
    }

    public new DialogResult ShowDialog(IWin32Window owner)
    {
        Init();
        return base.ShowDialog(owner);
    }


    public void Init()
    {
        // code goes here
    }
}