Mark
Mark

Reputation: 25

C# Skip or stop next function

I have a button. One button has three functions and I want skip last function when there is a case of function one or two.

I have tried return; but nothing. or it disables everything, or still doing its thing

Here is example:

     private bool functionOne() 
     {
        if (blah blah == "" || blah blah2 == "" || blah blah3 == "")
        {
            MessageBox.Show("text");
            return true;
        }
        try
        {

            if (count == 1)
            {
                MessageBox.Show("Text");
                return true;
            }
            else
            {
                return false;
            }


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private bool functionTwo() 
    {
        if (blah blah == "" || blah blah2 == "" || blah blah3 == "")
        {
            MessageBox.Show("text");
            return true;
        }
        try
        {

            if (count == 1)
            {
                MessageBox.Show("text");
                return true;
            }
            else
            {
                return false;
            }


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void functionThree() 
    {
        if (blah blah == "" || blah blah2 == "" || blah blah3 == "")
        {
            MessageBox.Show("text");
            return true;
        }
        try
        {

            if (count == 1)
            {
                MessageBox.Show("Text");
                return true;
            }
            else
            {
                return false;
            }


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


     private void Button_Click(object sender, EventArgs e) 
    {
       bool result1 = functionOne();
       bool result2 = functionTwo();
       if (result1|| result2) functionThree();
    }

What should I add into Function one and Two at the end instead of return; so that I can skip FunctionThree at all?

Upvotes: 0

Views: 1223

Answers (1)

Theodor Zoulias
Theodor Zoulias

Reputation: 43545

Change the first two functions to return a boolean result, so that you can do this:

private void Button_Click(object sender, EventArgs e) 
{
   bool result1 = FunctionOne()
   bool result2 = FunctionTwo()
   if (result1 || result2) FunctionThree()
}

Example of how to modify the FunctionOne and FunctionTwo:

private bool FunctionOne()
{
    if (MessageBox.Show("Something One?", "Caption", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Upvotes: 1

Related Questions