-3

I want to assign a data to an int volunteerEducation if I Checked radio button to save it to the database!

            int volunteerEducation;
            switch (volunteerEducation)
                case radioButton9.Checked:
                    volunteerEducation= 9;
                    break;
                case radioButton10.Checked:
                    volunteerEducation = 10;
                    break;
Abdullah Bahattab
  • 612
  • 1
  • 16
  • 32
  • You should read the [documentation](http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.90).aspx) to learn when and how to use the switch statement. –  Oct 24 '13 at 08:24
  • Possible duplicate http://stackoverflow.com/questions/1797907/which-radio-button-in-the-group-is-checked – Zaksh Oct 24 '13 at 08:24
  • NO its not duplicated. you can see the answer from Shahid Iqbal – Abdullah Bahattab Oct 24 '13 at 08:46
  • I could not find the answer which you are talking about. http://stackoverflow.com/questions/1797907/which-radio-button-in-the-group-is-checked – Abdullah Bahattab Oct 24 '13 at 08:54
  • You can achieve this by using LINQ mentioned by SLaks with little tweak var checkedButton = this.Controls.OfType() .FirstOrDefault(r => r.Checked); – Zaksh Oct 24 '13 at 10:04
  • I have never use LINQ before, Sorry. But I solve it now by simple way, you can see my answer it is basic and old way but it's effect and works properly – Abdullah Bahattab Oct 25 '13 at 12:12

3 Answers3

1

try this...

  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
     }
    }
Shahid Iqbal
  • 2,095
  • 8
  • 31
  • 51
  • It should be `foreach(Control c in this.Controls)` if radio buttons are placed directly on form. – Software Engineer Oct 24 '13 at 08:34
  • 1
    @AbdullahBahattab Welcome to Stack Exchange. You are of course free to accept whichever answer you want (I'm not jealous or anything like that) *but* it's usually a good idea to wait at least a day before accepting an answer, to allow users from other time zones to answer as well. Accepting an answer basically says "this question has been answered to my satisfaction" and hence reduces the likelihood of additional answers, even though there may be better techniques to accomplish the stated goal. – user Oct 24 '13 at 08:52
  • OK, sorry for that, I'm a new member just now. let us see the best solution (Answer). – Abdullah Bahattab Oct 24 '13 at 09:37
  • @AbdullahBahattab Just keep in mind that you should accept the answer that was *most helpful to you in answering your question or solving your problem*. That does not necessarily correspond to the answer that has been highest voted by the community, although it often does. Again, welcome. – user Oct 24 '13 at 11:00
0

switch allows you to check for one of several values on a single variable. if statements allow arbitrary conditions, which can check a single variable or many. Hence, switch is unsuited for what you are trying to do.

There are a few obvious options:

  • Use a series of if ... else if statements
  • Use some fancier construct such as a lookup dictionary mapping to methods (Dictionary<RadioButton,Func<>> might be a good place to start), though if you are unfamiliar with such constructs as switch I'd strongly recommend against that (it's the old "you have to learn to walk before you start to run")
  • Iterate over the radio buttons and do your magic inside a loop

Such a loop would look like:

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)
        {
            // ...
        }
    }
}

Using as rather than is followed by an explicit cast is idiomatic, and also saves you a cast; null checking is almost certainly faster, and certainly not slower, than casting. The as operator gives you null if the given value cannot be cast to the given type. In the case where the value of the variable you are working can be changed from another thread (not likely in this case, but possible in other cases), it also saves you the headache of a nigh-impossible-to-reproduce bug when something changes the value of the variable out under your feet.

A set of if statements to do the same thing would be along the lines of

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

Using else if mimics the behavior of a switch ... case ... break construct. If they are independent, simply omit the else. (Then, I'd suggest adding a blank line between the end of the if statement-block and the next if statement, for readability.)

user
  • 6,897
  • 8
  • 43
  • 79
0

I found the Answer and it's not really different from thus answers. the Point is to declare int. e.g int volunteerEducation = 0; then you have to use 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);
        }
    } 
Abdullah Bahattab
  • 612
  • 1
  • 16
  • 32