tags:

views:

32

answers:

2

I have a simple application:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

Constructor of Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // ...

        if (some_condition)
        {
            DialogResult dr = MessageBox.Show("Do you want to continue ?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);

            if (dr == DialogResult.No)
            {
                // How to close the window normally or how to not create a Form1 instance ?
                //
                //
            }
        }

        // amount of code that executes only if some_condtion == false
    }
}

I know that I can check some_condition before Application.Run but it's difficult to change (believe me). I need to check some_condition in constructor of Form1. If some_condition == true and answer is no --> application closes.

+1  A: 

It will be easier to accomplish this in the form load event. You can just call Close().

Calling Close() in the constructor will cause problems because the window is not yet open. You'll end up with a disposed window that is still trying to open.

msergeant
A: 

The constructor purpose is to create an instance of an object. Therefore, it should not fail unless some premise of the object fails. In this particular case, your only option is to thrown an exception.

In other words: you call the constructor of an object in order to create an instance of this object, so this is an atomic function and should not fail unless you have a technical problem.

Now, what you want is to avoid the form being displayed under some conditions. There are some ways of doing this. First, let's look at the code:

Application.Run(new Form1());

This snippet is doing two things: creating an instance of Form1 and displaying it (by feeding it to Application.Run). So one option is:

// snippet at program.cs
Form1 mainForm = new Form1();
if (mainForm.IsValid)
{
     Application.Run(new Form1());        
}

// snippet at Form1.cs
public bool IsValid
{
    get 
    {
        // evaluate all conditions that should determine if the form is to be showed.
        return condition;   
    }
}

You add code at program.cs to check if the form is valid. One way is to expose this as a property in the form. Of course, if you are following separation of concerns, maybe this would be a method/property of your domain, but it's your call where to put it in.

This method/property (called "IsValid" in my code) has the responsibility to check if the form can be displayed at that time.

Remember to decouple logic from presentation in your program.

That would do it.

Bruno Brant
thank you, Bruno.
alexander
Sorry for long-time asnwer, i thought i checked your answer as useful.
alexander
@alexander: hey, no problem! Thanks, I hope the answer was useful.
Bruno Brant