Reputation: 61
I have many if and else statements and I am wondering how I can make it short and sweet. This function checks if the answer the user input into the textbox is the same as the answer in the (hidden) datagrid. If it is the same add 1 to correctAnswer - which calculates how many correct answers the user has correct (vice versa for wrong answer)
bool firstAnswerCorrect = CheckAnswer(dataGridView1.Rows[0], textBoxQ1);
if (firstAnswerCorrect == true)
{
label1.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label1.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool secondAnswerCorrect = CheckAnswer(dataGridView1.Rows[1], textBoxQ2);
if (firstAnswerCorrect == true)
{
label2.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label2.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool thirdAnswerCorrect = CheckAnswer(dataGridView1.Rows[2], textBoxQ3);
if (thirdAnswerCorrect == true)
{
label3.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label3.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool fourthAnswerCorrect = CheckAnswer(dataGridView1.Rows[3], textBoxQ4);
if (fourthAnswerCorrect == true)
{
label4.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label4.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool fifthAnswerCorrect = CheckAnswer(dataGridView1.Rows[4], textBoxQ5);
if (fifthAnswerCorrect == true)
{
label5.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label5.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool sixthAnswerCorrect = CheckAnswer(dataGridView1.Rows[5], textBoxQ6);
if (sixthAnswerCorrect == true)
{
label6.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label6.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool seventhAnswerCorrect = CheckAnswer(dataGridView1.Rows[6], textBoxQ7);
if (seventhAnswerCorrect == true)
{
label7.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label7.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool eighthAnswerCorrect = CheckAnswer(dataGridView1.Rows[7], textBoxQ8);
if (eighthAnswerCorrect == true)
{
label8.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label8.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool ninethAnswerCorrect = CheckAnswer(dataGridView1.Rows[8], textBoxQ9);
if (ninethAnswerCorrect == true)
{
label9.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label9.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
bool tenthAnswerCorrect = CheckAnswer(dataGridView1.Rows[9], textBoxQ10);
if (tenthAnswerCorrect == true)
{
label10.Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
label10.Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
label11.Text = ("YOU HAVE SCORED " + correctAnswers + " OUT OF 10");
label12.Text = ("YOU HAVE " + wrongAnswers + " QUESTIONS WRONG");
The code WORKS its just repetitive
EDIT:
This function just calculates the correct and incorrect answers. I have another function which checks if the answer the user input into the textbox is the same as the answer in the (hidden) datagrid
This is the code for that function:
private bool CheckAnswer(DataGridViewRow dataGridViewRow, TextBox textBox)
{
string correctAnswer = dataGridViewRow.Cells["answer"].Value.ToString();
string givenAnswer = textBox.Text;
bool isCorrect = string.Equals(correctAnswer, givenAnswer, StringComparison.CurrentCultureIgnoreCase);
return isCorrect;
}
As I said. Its working but its just repetitive which is not a good sign.
EDIT:
This is the new C# code I am using which is eliminates the code duplication, however I encountered an exception when I tweaked it a bit.
List<TextBox> textboxes = new List<TextBox> { textBoxQ1, textBoxQ2, textBoxQ3, textBoxQ4, textBoxQ5, textBoxQ6, textBoxQ7, textBoxQ8, textBoxQ9, textBoxQ10 };
List<Label> labels = new List<Label> { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10 };
bool temp;
for (int i = 0; i < 10; i++)
{
temp = CheckAnswer(dataGridView1.Rows[i], textboxes[i]);
if (temp == true)
{
labels[i].Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
labels[i].Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
label11.Text = ("YOU HAVE SCORED " + correctAnswers);
label12.Text = ("YOU HAVE SCORED " + correctAnswers);
}
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
The line:
temp = CheckAnswer(dataGridView1.Rows[i], textboxes[i]);
Upvotes: 1
Views: 192
Reputation: 4000
You can make one List<TextBox>
and one another List<Label>
like below:
List<TextBox> textboxes = new List<TextBox>{textbox1, ....}
List<Label> labels = new List<Label>{label1, label2, ....}
bool temp;
for(int i = 0; i < 10; i++)
{
temp = CheckAnswer(dataGridView1.Rows[i], textBoxes[i]);
if (temp)
{
labels[i].Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
labels[i].Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
}
Upvotes: 5
Reputation: 52
A simple solution would be changing everything to strings instead of Booleans. This way you won't need any if
statements. Just label1.Text = firstAnswerCorrect
which equals (string) either "correct"
or "incorrect"
And for the function you would just return "correct"
or "incorrect"
Hope this helps.
Upvotes: -2
Reputation: 4226
May try to reduce repetition in the following way:
List<TextBox> textBoxList = new List<TextBox>() {
textBoxQ1,
textBoxQ2,
...
textBoxQN
};
List<Label> labelList = new List<Label>() {
label1,
label2,
...
labelN
};
for(int i = 0; i < textBoxList.Count; i++) {
bool answerCorrect = CheckAnswer(dataGridView1.Rows[i], textBoxList[i]);
labelList[i].Text = answerCorrect ? "correct" : "incorrect";
correctAnswers += answerCorrect ? 1 : 0;
wrongAnswers += !answerCorrect ? 1 : 0;
}
Upvotes: 1
Reputation: 7579
I'd store all the answer textboxes and correct/incorrect answer labels in an array, like Label[] answerLabels
and TextBox[] answerTextBoxes
. These arrays should be properly ordered, i.e. answerLabels[0]
and answerTextBoxes[0]
should correspond to the 1st question, answerLabels[1]
and answerTextBoxes[1]
should correspond to the 2nd question and so on.
Thereafter this becomes a single for loop:
for(int i = 0; i < answerLabels.Length; i++) {
bool answerCorrect = CheckAnswer(dataGridView1.Rows[i], answerTextBoxes[i]);
if (answerCorrect == true)
{
answerLabels[i].Text = "correct";
correctAnswers = correctAnswers + 1;
}
else
{
answerLabels[i].Text = "incorrect";
wrongAnswers = wrongAnswers + 1;
}
}
Upvotes: 0