-1
\$\begingroup\$

I heard goto is bad, so I ask someone in this site if my code is better using goto or other alternative

    public FormMain()
    {
    // start of the apps
    Start:

        // show login form
        FormLogin login = new FormLogin();
        DialogResult dr = login.ShowDialog();

        // cancel form loading if login form is canceled
        if (dr == DialogResult.Cancel)
        {
            Load += (s, e) => Close();
            return;
        }

        // login error
        if (dr == DialogResult.No)
        {
            // back to start
            goto Start;
        }

        // Initialize main form component
        InitializeComponent();
    }
\$\endgroup\$

closed as off-topic by Mast, Sᴀᴍ Onᴇᴌᴀ, Dannnno, hjpotter92, Stephen Rauch Mar 29 '18 at 18:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Mast, Sᴀᴍ Onᴇᴌᴀ, Dannnno, hjpotter92, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.

5
\$\begingroup\$

You don't want to use goto in this case. Use a loop instead, like this:

public FormMain()
{
    DialogResult dr;
    do
    {
        // show login form
        FormLogin login = new FormLogin();
        dr = login.ShowDialog();

        // cancel form loading if login form is canceled
        if (dr == DialogResult.Cancel)
        {
            Load += (s, e) => Close();
            return;
        }
    }
    while(dr == DialogResult.No); // login error

    // Initialize main form component
    InitializeComponent();
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.