ZaiFrost ZaiFrost - 4 days ago 5
C# Question

If Statement with Switch-Case Never Executed?

I created a Quiz Application using WinForms. In my Form1, I created a ComboBox containing the different categories the user can choose from, which will influence what type of Questions will get printed out from the if-else statements and switch-cases I have in my Quiz Form.

The if-else statement

if (f1.comboBox1.SelectedIndex == 1)
and the following others are the ones that never execute, only the
else
followed by the Switch-Case statement.

(I know my code can be improved by a lot, but I just want to mess around with coding for now and would appreciate if you could help me to solve this problem.)

Here is the code which I suspect to affect it:

public int i = 0;
int score = 0;
static Random gen = new Random();
int[] rand0 = Enumerable.Range(1, 20).OrderBy(r => gen.Next()).ToArray();
int[] rand1 = Enumerable.Range(6, 5).OrderBy(r => gen.Next()).ToArray();
int[] rand2 = Enumerable.Range(11, 5).OrderBy(r => gen.Next()).ToArray();
int[] rand3 = Enumerable.Range(16, 5).OrderBy(r => gen.Next()).ToArray();

public Quiz()
{
InitializeComponent();
}

private void startButton_Click(object sender, EventArgs e)
{
try
{
if (f1.comboBox1.SelectedIndex == 1)
{
switch (rand1[i])
{
case 1:
Question1();
i++;
break;
case 2:
Question2();
i++;
break;
case 3:
Question3();
i++;
break;
case 4:
Question4();
i++;
break;
case 5:
Question5();
i++;
break;
case 6:
Question6();
i++;
break;
case 7:
Question7();
i++;
break;
case 8:
Question8();
i++;
break;
case 9:
Question9();
i++;
break;
case 10:
Question10();
i++;
break;
case 11:
Question11();
i++;
break;
case 12:
Question12();
i++;
break;
case 13:
Question13();
i++;
break;
case 14:
Question14();
i++;
break;
case 15:
Question15();
i++;
break;
case 16:
Question16();
i++;
break;
case 17:
Question17();
i++;
break;
case 18:
Question18();
i++;
break;
case 19:
Question19();
i++;
break;
case 20:
Question20();
i++;
break;
}
if (i == 5)
b.Text = "Finish";

if (i != 5)
b.Text = "Next";
}
else if (f1.comboBox1.SelectedIndex == 2)
{
switch (rand2[i])
{
case 1:
Question1();
i++;
break;
case 2:
Question2();
i++;
break;
case 3:
Question3();
i++;
break;
case 4:
Question4();
i++;
break;
case 5:
Question5();
i++;
break;
case 6:
Question6();
i++;
break;
case 7:
Question7();
i++;
break;
case 8:
Question8();
i++;
break;
case 9:
Question9();
i++;
break;
case 10:
Question10();
i++;
break;
case 11:
Question11();
i++;
break;
case 12:
Question12();
i++;
break;
case 13:
Question13();
i++;
break;
case 14:
Question14();
i++;
break;
case 15:
Question15();
i++;
break;
case 16:
Question16();
i++;
break;
case 17:
Question17();
i++;
break;
case 18:
Question18();
i++;
break;
case 19:
Question19();
i++;
break;
case 20:
Question20();
i++;
break;
}
if (i == 5)
b.Text = "Finish";

if (i != 5)
b.Text = "Next";
}
else if (f1.comboBox1.SelectedIndex == 3)
{
switch (rand3[i])
{
case 1:
Question1();
i++;
break;
case 2:
Question2();
i++;
break;
case 3:
Question3();
i++;
break;
case 4:
Question4();
i++;
break;
case 5:
Question5();
i++;
break;
case 6:
Question6();
i++;
break;
case 7:
Question7();
i++;
break;
case 8:
Question8();
i++;
break;
case 9:
Question9();
i++;
break;
case 10:
Question10();
i++;
break;
case 11:
Question11();
i++;
break;
case 12:
Question12();
i++;
break;
case 13:
Question13();
i++;
break;
case 14:
Question14();
i++;
break;
case 15:
Question15();
i++;
break;
case 16:
Question16();
i++;
break;
case 17:
Question17();
i++;
break;
case 18:
Question18();
i++;
break;
case 19:
Question19();
i++;
break;
case 20:
Question20();
i++;
break;
}
if (i == 5)
b.Text = "Finish";

if (i != 5)
b.Text = "Next";
}
else
{
switch (rand0[i])
{
case 1:
Question1();
i++;
break;
case 2:
Question2();
i++;
break;
case 3:
Question3();
i++;
break;
case 4:
Question4();
i++;
break;
case 5:
Question5();
i++;
break;
case 6:
Question6();
i++;
break;
case 7:
Question7();
i++;
break;
case 8:
Question8();
i++;
break;
case 9:
Question9();
i++;
break;
case 10:
Question10();
i++;
break;
case 11:
Question11();
i++;
break;
case 12:
Question12();
i++;
break;
case 13:
Question13();
i++;
break;
case 14:
Question14();
i++;
break;
case 15:
Question15();
i++;
break;
case 16:
Question16();
i++;
break;
case 17:
Question17();
i++;
break;
case 18:
Question18();
i++;
break;
case 19:
Question19();
i++;
break;
case 20:
Question20();
i++;
break;
}
}
if (i == 15)
b.Text = "Finish";

if (i != 15)
b.Text = "Next";
} catch { }

b.Enabled = false;
}
}


}

Answer

Comboboxin Winforms is Zero based index - I hope you are aware of this, since you do not check for your 'first Index' ever... This would at least explain, why your else is being executed when your first element in ComboBox is selected MSDN ComboBox

Edit from comment: As you explained in comments you close your f1 form:

"You .Close() the form f1 ? If you do this, it's just natural that your index is -1.

Since -1 means that no value is selected which is obviously the case in a form which is closed. If you want your index in your sub-form, but your mainform is closed: pass it when calling your Quiz Form by Parameter"

Comments