Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Michael Hampshire
Michael Hampshire
12,206 Points

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,023 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).