Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Can I call two methods in an if statement at the same time?

Is it possible for me to call two different methods in a single if statement like so? (The very last if statement with gameOver() and reset().)

namespace UpOrDown
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int score = 0;
        int wrong = 0;
        int highScore = 0;
        Random random = new Random();


        public void correctGuess()
        {
            score++;
            scoreCounterLabel.Text = score.ToString();
        }

        public void wrongGuess()
        {
            wrong++;
            wrongScore.Text = wrong.ToString();
        }

        public void gameOver()
        {
            highScore = score;
            highscoreScore.Text = highScore.ToString();
            MessageBox.Show("Game Over");
        }

        public void reset()
        {
            scoreCounterLabel.Text = "0";
            wrongScore.Text = "0";
            score = 0;
            wrong = 0;
        }

        private void upButton_Click(object sender, EventArgs e)
        {
            int randomNumber = random.Next(0, 10);

            if (randomNumber >= 5 && wrong <= 3)
            {
                correctGuess();
            }
            else
            {
                wrongGuess();
            }

            if (wrong == 3)
            {
                gameOver();
            }
        }

        private void downButton_Click(object sender, EventArgs e)
        {
            int randomNumber = random.Next(0, 10);

            if (randomNumber <= 5 && wrong <= 3)
            {
                correctGuess();
            }
            else
            {
                wrongGuess();
            }

            if (wrong == 3)
            {
                gameOver();
                reset();
            }
        }
    }
}

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Yes.

Edit: You can call as much methods concurrently as you want, just make sure you know what each method is doing so that you don't run into unintended results (one method closing the program while the method after tries to access information from it). The only time you should really be concerned for running methods one after the other, is when you are working on multi-threaded applications (but that is topic for a different time).