2016-03-16 4 views
1

Привет, мне интересно, есть ли способ упростить следующий 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); 
} 
+1

Подсказка: число вы используете для увеличения 'GATotal' такое же, как число, которое вы используете для индексации' RadioButtonList's. Вы можете получить индекс выбранной кнопки, используя 'RadioButtonList.SelectedIndex' – Rik

ответ

1

Вы в основном добавив индекс элемента в GATOTAL, почему бы не использовать SelectedIndex в RadioButtonList непосредственно

например

GATotal += RadioButtonList.SelectedIndex + 
      RadioButtonList1.SelectedIndex + 
      RadioButtonList2.SelectedIndex + 
      RadioButtonList3.SelectedIndex + 
      RadioButtonList4.SelectedIndex + 
      RadioButtonList5.SelectedIndex; 
0
GATotal += RadioButtonList.SelectedIndex + RadioButtonList1.SelectedIndex ... 
0

Такой подход позволит сделать это легко, если вы хотите назначить различные значения для выбранных элементов:

protected void GA_Total(object sender, EventArgs e) 
{ 
    // Generalized Anxiety Total 
    int GATotal = 0; 

    GATotal += sumSelectedItems(radioButtonList); 
    GATotal += sumSelectedItems(radioButtonList1); 
    GATotal += sumSelectedItems(radioButtonList2); 
    GATotal += sumSelectedItems(radioButtonList3); 
    GATotal += sumSelectedItems(radioButtonList4); 
    GATotal += sumSelectedItems(radioButtonList5); 

    Label1.Text = GATotal.toString(); 
} 


private int sumSelectedItems(RadioButtonList list) 
{ 
    int sum = 0; 
    for (int i = 0; i < list.Items.Count; i++) 
    { 
     if (list.Items[i].Selected) 
     { 
      sum += i; 
     } 
    } 

    return sum; 
} 
0

Предполагая, что у вас есть все переключатели в таблице

<asp:Table ID="Table1" runat="server"><asp:RadioButtonList runat="server"></asp:RadioButtonList></asp:Table> 

Ниже приведен код для любого количества радиокнопков.

foreach (Control ctrl in Table1.Controls) 
{ 
    if (ctrl is RadioButtonList) 
    { 
    RadioButtonList rbl = (RadioButtonList)ctrl; 

    for (int i = 0; i < rbl.Items.Count; i++) 
    { 
     if (rbl.Items[i].Selected) 
     { 
      GATotal+= RadioButtonList.SelectedIndex; 

     } 
    } 
} 

}