Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have multiple if - else sttments involving radiobuttons. They are used in condition part of the if - else stements. How can I convert these multiple if - else sttments into a switch sttment?
Please help.
Jyoti Bhatnagar
Posted 10-Oct-12 13:02pm
Jyoti5356
Comments
Sergey Alexandrovich Kryukov at 10-Oct-12 18:29pm
   
Why? What's the problem? What did you try so far? Any code sample?
--SA
Sergey Alexandrovich Kryukov at 10-Oct-12 18:49pm
   
It's simper -- alternative choice only! Please see my answer.
--SA
Jyoti5 at 10-Oct-12 18:52pm
   
Hey SA,
I dont see ur answer. Can u please send it again.
By Mistake I deleted the comment
Thanks,
Jyoti
Sergey Alexandrovich Kryukov at 10-Oct-12 18:54pm
   
Refresh the page, to see it :-)
--SA
Jyoti5 at 11-Oct-12 18:12pm
   
I have posted the code. It does not work.
Sergey Alexandrovich Kryukov at 11-Oct-12 18:25pm
   
First, if you need to post your code, use "Improve question" above and remove it from comment, to improve readability. In this case, you can use <pre lang="cs"> tag around code to show it formatted properly.
 
You code is totally wrong. Not only you use immediate constants "rdoBtnMakeOver", "rdoBtnTen", but you even repeat them. At least declare them as constants explicitly (do you know the key word "const"? :-) Do you understand that this in unsupportable. Look at my answer: you don't need switch at all, as you only need to find out which radio button is selected. Put a tag in each, find the selected one and use the tag in the statement decIndvdualSrvceChrg = //...
Only one line.
 
But first of all, please understand that software developers don't say "it does not work", they provide comprehensive issue report. Don't do it just yet, first, re-write the code in humanly manner.
--SA
Sarvesh Kushwaha at 11-Oct-12 0:15am
   
can you provide some snippet of your code so we guys can help you ......
Jyoti5 at 11-Oct-12 18:08pm
   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace template
{
public partial class Form1 : Form
{
const decimal
decMAKE_OVER_PRICE = 125.0M,
decHAIR_STYLING_PRICE = 60.0M,
decMANICURE_PRICE = 35.0M,
decPERM_MAKEUP_PRICE = 200.0M,
decTEN_DISCOUNT = 0.10M,
decTWENTY_DISCOUNT = 0.20M,
decNONE_DISCOUNT = 0.0M;
 
public Form1()
{
InitializeComponent();
}
 
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
 
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal decSrvceChrg, decIndvdualSrvceChrg = 0.0M, decToalVisitChrg;
string strMsg;
//Find price for each type of service
RadioButton btn = sender as RadioButton;
RadioButton btn1 = sender as RadioButton;
if (btn != null && btn.Checked)
{
switch (btn.Name)
{
case "rdoBtnMakeOver":
decSrvceChrg = decMAKE_OVER_PRICE;
if (btn1 != null && btn1.Checked)
{
switch (btn1.Name)
{
case "rdoBtnTen":
decIndvdualSrvceChrg = decSrvceChrg - (decSrvceChrg * decTEN_DISCOUNT);
break;
case "rdoBtnTwenty":
decIndvdualSrvceChrg = decSrvceChrg - (decSrvceChrg * decTWENTY_DISCOUNT);
break;
case "rdoBtnNone":
decIndvdualSrvceChrg = decSrvceChrg - (decSrvceChrg * decNONE_DISCOUNT);
break;
}
}
break;
 
case "rdoBtnHairStyle":
decSrvceChrg = decHAIR_STYLING_PRICE;
if (btn1 != null && btn1.Checked)
{
switch (btn1.Name)
{
case "rdoBtnTen":
decIndvdualSrvceChrg = decSrvceChrg - decSrvceChrg * decTEN_DISCOUNT;
break;
case "rdoBtnTwenty":
decIndvdualSrvceChrg = decSrvceChrg - decSrvceChrg * decTWENTY_DISCOUNT;
break;
case "rdoBtnNone":
decIndvdualSrvceChrg = decSrvceChrg - decSrvceChrg * decNONE_DISCOUNT;
break;
default:
MessageBox.Show("Please make a discount selection", "Discount Selection Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
break;
case "rdoBtnManicure":
decSrvceChrg = decMANICURE_PRICE;
if (btn1 != null && btn1.Checked)
{
switch (btn1.Name)
{
case "rdoBtnTen":
decIndvdualSrvceChrg = decSrvceChrg - decSrvceChrg * decTEN_DISCOUNT;
break;
case "rdoBtnTwenty":
decIndvdualSrvceChrg = decSrvceChrg - decSrvceChrg * decTWENTY_DISCOUNT;
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You probably don't take into account the simple fact that the radio buttons work like a single choice (unlike check boxes, for example), if they are placed in the same parent control, like a panel, form (window) or a group box. When one is selected, others are always unselected. This makes the code much simpler, as you don't need your nested checks and even else if. Are you getting the idea?
 
And that makes writing a single case statement really simple, because there is only one case at a time.
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

All you need to do with the radio button is select the event tab under properties and select radioButton_Click under the "Checked" event. I hope this will be helpful to someone in the future.
 

private void radioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioBtn = (RadioButton)sender;
            if (radioBtn.IsChecked == true)
            {
                switch (radioBtn.Name)
                {
                    case "radioBtnName1":
                        //do something
                        break;
 
                    case "radioBtnName2":
                        //do something
                        break;
 
                    case "radioBtnName3":
                        //do something
                        break;
 
                    case "radioBtnName4":
                        //do something
                        break;
                }
 
            }
 

        }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100