-2

我想将数据分配给 int volunteerEducation 如果我选中单选按钮以将其保存到数据库中!

            int volunteerEducation;
            switch (volunteerEducation)
                case radioButton9.Checked:
                    volunteerEducation= 9;
                    break;
                case radioButton10.Checked:
                    volunteerEducation = 10;
                    break;
4

3 回答 3

1

尝试这个...

  foreach(Control c in this.Controls)
    {
     if(c is RadioButton)
     {
      RadioButton rbtn = (RadioButton)c;
      // now here you can use if else statements or Switch Statement
     }
    }
于 2013-10-24T08:27:27.060 回答
0

switch允许您检查单个变量的多个值之一。if语句允许任意条件,可以检查单个变量或多个变量。因此,switch不适合您尝试做的事情。

有几个明显的选择:

  • 使用一系列if ... else if语句
  • 使用一些更花哨的构造,例如查找字典映射到方法(Dictionary<RadioButton,Func<>>可能是一个不错的起点),但如果您不熟悉诸如 switch 之类的构造,我强烈建议您不要这样做(这是旧的“你必须学会​​走路在你开始跑步之前”)
  • 遍历单选按钮并在循环中施展你的魔法

这样的循环看起来像:

foreach (Control c in this.Controls)
{
    RadioButton rbtn = c as RadioButton;
    if(rbtn != null)
    {
        // ... your code to work with 'rbtn' goes here ...
        if (rbtn.Checked)
        {
            // ...
        }
    }
}

使用as而不是is后跟显式演员表是惯用的,也可以为您节省演员表;空值检查几乎肯定比强制转换更快,而且肯定不会慢。如果给定的值不能转换为给定的类型,则as运算符会为您提供。null如果您正在工作的变量的值可以从另一个线程更改(在这种情况下不太可能,但在其他情况下可能),它还可以让您避免在某些情况下出现几乎不可能重现的错误改变你脚下的变量的值。

一组if做同样事情的语句将沿着

if (radioButton9.Checked)
{
    // do something
}
else if (radioButton10.Checked)
{
    // do something
}

使用else if模仿switch ... case ... break构造的行为。如果它们是独立的,只需省略else. (然后,为了便于阅读,我建议在 if 语句块的末尾和下一个 if 语句之间添加一个空行。)

于 2013-10-24T08:50:12.620 回答
0

我找到了答案,它与因此的答案并没有什么不同。重点是声明int。例如 int 志愿者教育 = 0;那么你必须使用 if ... else if。

private void updateButton_Click(object sender, EventArgs e)
    {
        try
        {
            string volunteerID = updtIDTextBox.Text;
            string volunteerName = updtNameTextBox.Text;
            string volunteerZone = updtZoneTextBox.Text;
            string volunteerStreet = updtStreetTextBox.Text;
            int volunteerSex = updtMaleRadioButton.Checked ? 0 : 1;
            DateTime volunteerBirthday = updtBirthdayDateTimePicker.Value;

            if (updtHomePhoneTextBox.Text == "")
                updtHomePhoneTextBox.Text = "0";
            int volunteerHomePhone = int.Parse(updtHomePhoneTextBox.Text);

            if (updtWorkPhoneTextBox.Text == "")
                updtWorkPhoneTextBox.Text = "0";
            int volunteerWorkPhone = int.Parse(updtWorkPhoneTextBox.Text);

            if (updtMobile1TextBox.Text == "")
                updtMobile1TextBox.Text = "0";
            int volunteerMobile1 = int.Parse(updtMobile1TextBox.Text);

            if (updtMobile2TextBox.Text == "")
                updtMobile2TextBox.Text = "0";
            int volunteerMobile2 = int.Parse(updtMobile2TextBox.Text);
            string volunteerEmail = updtEmailTextBox.Text;
            string volunteerJob = updtJobTextBox.Text;
            string volunteerAffiliation = updtAffiliationTextBox.Text;
            //The solution start from here
            int volunteerEducation = 0;
            if (radioButton10.Checked)
                volunteerEducation = 1;
            else if (radioButton11.Checked)
                volunteerEducation = 2;
            else if (radioButton12.Checked)
                volunteerEducation = 3;
            else if (radioButton14.Checked)
                volunteerEducation = 5;
            else if (radioButton15.Checked)
                volunteerEducation = 6;
            else if (radioButton16.Checked)
                volunteerEducation = 7;
            else if (radioButton17.Checked)
                volunteerEducation = 8;
            else if (radioButton18.Checked)
                volunteerEducation = 9;
            //end solution
            string volunteerEducationPlace = addEducationPlaceTextBox.Text;
            string volunteerEducationDepartmen = addEducationDepartmentTextBox.Text;
            //same above
            int volunteerInteresting = 0;
            if (updtIntrstMdcnRadioButton.Checked)
                volunteerInteresting = 1;
            else if (updtIntrstSosclRadioButton.Checked)
                volunteerInteresting = 2;
            else if (updtIntrstLrnRadioButton.Checked)
                volunteerInteresting = 3;
            else if (updtIntrstTrnRadioButton.Checked)
                volunteerInteresting = 4;
            //end
            string volunteerNotes = addNotesTextBox.Text;

            int x = dataGridViewX1.SelectedRows[0].Index;
            UpdateVolunteer(volunteerID, volunteerName, volunteerZone, volunteerStreet, volunteerSex, volunteerBirthday, volunteerHomePhone, volunteerWorkPhone, volunteerMobile1, volunteerMobile2, volunteerEmail, volunteerJob, volunteerAffiliation, volunteerEducation, volunteerEducationPlace, volunteerEducationDepartmen, volunteerInteresting, volunteerNotes);
            showVolunteers(x);

            updtIDTextBox.Text = "";
            updtNameTextBox.Text = "";
            updtZoneTextBox.Text = "";
            updtStreetTextBox.Text = "";
            updtBirthdayDateTimePicker.Value = DateTime.Now;
            updtHomePhoneTextBox.Text = "";
            updtWorkPhoneTextBox.Text = "";
            updtMobile1TextBox.Text = "";
            updtMobile2TextBox.Text = "";
            updtEmailTextBox.Text = "";
            updtJobTextBox.Text = "";
            updtAffiliationTextBox.Text = "";
            updtEducationPlaceTextBox.Text = "";
            updtEducationDepartmentTextBox.Text = "";
            updtNotesTextBox.Text = "";


        }
        catch (SqlException sql)
        {
            MessageBoxEx.Show(sql.Message);
        }
    } 
于 2013-10-25T11:34:45.357 回答