Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 ASP.NET4.0
Currently in my web page I have one button and nearly 20 text boxes. Each text box has its default value(say Textbox1="1" and Textbox2="2"....Textbox20="20"). if i type 1 in Textbox1(ie.the default value),then i have to show that my "Correct answer =1" and "wrong Answer =0). If i typed all answer right then my "Correct Answer=20" and Wrong Answer=0"..Instead of typing 1 in Texbox1 if i type 5,then that is considered as Wrong answer. when i finish typing in all these 20 textboxes i have to click a button where i have to show the status of correct answer and wrong answer.. i already did this by using "if else"..i'm pasting my code below.. i want to know is there any other way to make this possible ....
 
in my button click event
 
      int _correct = 0;
        int _wrong = 0;
        
        
        if (text2.Text == "2")
        {
            _correct++;
        }
        else
        {
            _wrong++;
        }
        if (text3.Text == "5")
        {
            _correct++;
        }
        else
        {
            _wrong++;
        }
.
.
.
.
.
lblCorrect.Visible = true;
           lblCorrect.Text = _correct.ToString();
           lblWrong.Visible = true;
           lblWrong.Text = _wrong.ToString();
Posted 27-Dec-12 0:02am
shamjid1.5K
Comments
__TR__ at 27-Dec-12 6:12am
   
One option would be to calculate Wrong answer as
20 - Correct answer
there by avoiding the else block.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can avoid the else block by using the following approach.
 
int _correct = 0;
        int _wrong = 20;
 

        if (text2.Text == "2")
        {
            _correct++;
            _wrong--;
        }
        if (text3.Text == "5")
        {
            _correct++;
        _wrong--;
        }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100