Step 4: Add the CheckTheAnswer() Method


For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

In the fourth part of this tutorial, you'll write a method, CheckTheAnswer(), that determines whether the answers to the math problems are correct. This topic is part of a tutorial series about basic coding concepts. For an overview of the tutorial, see Tutorial 2: Create a Timed Math Quiz.

System_CAPS_ICON_note.jpg Note

If you're following along in Visual Basic, you'll use the Function keyword instead of the usual Sub keyword because this method returns a value. It's really that simple: a sub doesn't return a value, but a function does.

To verify whether the answers are correct

  1. Add the CheckTheAnswer() method.

    When this method is called, it adds the values of addend1 and addend2 and compares the result to the value in the sum NumericUpDown control. If the values are equal, the method returns a value of true. Otherwise, the method returns a value of false. Your code should look like the following.

        /// <summary>
        /// Check the answer to see if the user got everything right.
        /// </summary>
        /// <returns>True if the answer's correct, false otherwise.</returns>
        private bool CheckTheAnswer()
            if (addend1 + addend2 == sum.Value)
                return true;
                return false;

    Next, you'll check the answer by updating the code in the method for the timer's Tick event handler to call the new CheckTheAnswer() method.

  2. Add the following code to the if else statement.

        private void timer1_Tick(object sender, EventArgs e)
            if (CheckTheAnswer())
                // If CheckTheAnswer() returns true, then the user 
                // got the answer right. Stop the timer  
                // and show a MessageBox.
                MessageBox.Show("You got all the answers right!",
                startButton.Enabled = true;
            else if (timeLeft > 0)
               // If CheckTheAnswer() return false, keep counting
               // down. Decrease the time left by one second and 
               // display the new time left by updating the 
               // Time Left label.
                timeLabel.Text = timeLeft + " seconds";
                // If the user ran out of time, stop the timer, show 
                // a MessageBox, and fill in the answers.
                timeLabel.Text = "Time's up!";
                MessageBox.Show("You didn't finish in time.", "Sorry!");
                sum.Value = addend1 + addend2;
                startButton.Enabled = true;

    If the answer is correct, CheckTheAnswer() returns true. The event handler stops the timer, shows a congratulatory message, and then makes the Start button available again. Otherwise, the quiz continues.

  3. Save your program, run it, start a quiz, and provide a correct answer to the addition problem.

    System_CAPS_ICON_note.jpg Note

    When you enter your answer, you must either select the default value before you start to enter your answer, or you must delete the zero manually. You'll correct this behavior later in this tutorial.

    When you provide a correct answer, a message box opens, the Start button becomes available, and the timer stops.

To continue or review