Привет, мне интересно, есть ли способ упростить следующий IF-Statement. У меня есть 47 вопросов, и я действительно не хочу, чтобы это повторялось повсюду. Я не знаком с C# и хотел бы руку.Упрощение IF-заявлений на RadioButtonList
protected void GA_Total(object sender, EventArgs e)
{
// Generalized Anxiety Total
int GATotal = 0;
Label1.Text = Convert.ToString(GATotal);
//-----------Question 1----------------
if (RadioButtonList.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 13----------------
if (RadioButtonList1.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList1.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList1.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList1.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 22----------------
if (RadioButtonList2.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList2.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList2.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList2.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 27----------------
if (RadioButtonList3.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList3.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList3.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList3.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 35----------------
if (RadioButtonList4.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList4.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList4.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList4.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 37----------------
if (RadioButtonList5.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList5.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList5.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList5.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//------------Results-------------------
Label1.Text = Convert.ToString(GATotal);
}
Подсказка: число вы используете для увеличения 'GATotal' такое же, как число, которое вы используете для индексации' RadioButtonList's. Вы можете получить индекс выбранной кнопки, используя 'RadioButtonList.SelectedIndex' – Rik