0

I have a code like this in 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");
        }
    }

I'm pretty sure I messed up the condition syntax, but I would expect that one of these would happen:

  1. Compiler warns about errors/project fails to run.
  2. First message is shown
  3. Second message is shown.

But neither of these actually happens. I've been staring at this for an hour and resources I could find are pretty slim. If anyone with an experience could explain me what actually happens here?

Edit: Thanks to JMK's link I found out this is basically a wontfix bug popping up in VS debugger under Windows x64. Error fires if application is run outside of debugger.

Community
  • 1
  • 1
user1612992
  • 35
  • 1
  • 6

3 Answers3

3

Its silently erroring.

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

Comes back with "The settings property 'Database' was not found"

BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

Try adding your project namespace before the Properties

if (WindowsFormsApplication2.Properties.Settings.Default.Database != null)
Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
0

Propably an exception is thrown and not noticed by the debugger. This happens for Windows Forms projects on 64bit Windows Versions (and is not a behaviour specific to .NET but to Windows in general).

More details here: Visual Studio does not break at exceptions in Form_Load Event

Try pressing STRG + ALT + E and mark the checkbox "Thrown" for "Common Language Runtime Exceptions". Now the debugger will break on any exception in Form_Load()

Since I know about that my workaround is to completly avoid using the Load event.

Most of my Forms are Dialogs so I shadow the ShowDialog() method and call a Init() function.

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
    }
}
Community
  • 1
  • 1
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189