4 Replies - 513 Views - Last Post: 19 April 2011 - 01:00 AM Rate Topic: -----

#1 MohammedA.Gouda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-April 11

switch case and if

Posted 18 April 2011 - 01:32 AM

can i use switch case like if i wrote this code:
if(radiobutton1.checked=true)
{
}
else if (radiobutton2.checked=true)
{
}
else if (radiobutton3.checked=true)
{
}
else
{
}


can i write this in switch case ? how ?
Is This A Good Question/Topic? 0
  • +

Replies To: switch case and if

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: switch case and if

Posted 18 April 2011 - 01:36 AM

Hi,

No, you can't really. The whole idea of a switch is that you have one variable that can take on multiple different values (cases), and you check for esch of those cases.

In this example, your using three different variables (the three checked properties of the different radio buttons), and your only testing for true or false, meaning there would only be two cases anyway!

Plus, your code won't compile anyway, as you have used the assignment operator "=", instead of the equality operator "==", and "checked" should have a capital "C". C# is case-sensitive :).

This post has been edited by CodingSup3rnatur@l-360: 18 April 2011 - 01:42 AM

Was This Post Helpful? 3
  • +
  • -

#3 MohammedA.Gouda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-April 11

Re: switch case and if

Posted 18 April 2011 - 01:41 AM

View PostCodingSup3rnatur@l-360, on 18 April 2011 - 01:36 AM, said:

Hi,

No, you can't really. The whole idea of a switch is that you have one variable that can take on multiple different values (cases), and you check for esch of those cases.

In this example, your using three different variables (the three checked properties of the different radio buttons), and your only testing for true or false, meaning there would only be two (maxmimum of three) cases anyway!

Plus, your code won't compile anyway, as you have used the assignment operator "=", instead of the equality operator "==".



thx so much
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,215
  • Joined: 02-June 10

Re: switch case and if

Posted 18 April 2011 - 05:10 AM

*
POPULAR

To expand on that... You really shouldn't be building tons of code that so directly access the GUI states like that anyway. It's just sloppy and newbie-ish. Enums and properties are a much cleaner way to go with this and would allow you to use switch constructs.

You centralize all of your GUI comparisons to one block of code. Then everywhere else in your program you check against the property.
Now your form only makes the property Public: We never make the GUI elements public. Other forms/classes can determine and even set your property without ever accessing the radio buttons themselves.

 enum UserModes
        {
            USER = 0,
            ADMINISTRATOR = 1,
            MAINTENANCE = 2
        }

        private UserModes _myusermode = UserModes.USER; // Gives you a default setting

        public UserModes myUserMode
        {
            get { return _myusermode; }
            set
            {
                _myusermode = value;
                // Make the GUI match the value if programmatically changed
                if (value == UserModes.USER) rbUser.Checked = true;
                if (value == UserModes.MAINTENANCE) rbMaint.Checked = true;
                if (value == UserModes.ADMINISTRATOR) rbAdmin.Checked = true;
                // there are other ways to to this such as iNotify interface, but this is easy to follow
                // 
                //Other things to do every time the UserMode gets changed
                groupAdmin.Enabled = value == UserModes.ADMINISTRATOR;
                LogThis("Usermode changed");
                SetStatusText(value.ToString());
}
        }

        private void rbUser_CheckedChanged(object sender, EventArgs e)
        {
            if (rbUser.Checked) myUserMode = UserModes.USER;
            if (rbMaint.Checked) myUserMode = UserModes.MAINTENANCE;
            if (rbAdmin.Checked) myUserMode = UserModes.ADMINISTRATOR;
        }

        void SomeOtherMethod()
        {
            switch (myUserMode)
            {
                case UserModes.USER:
                    //do User stuff here
                    break;
                case UserModes.MAINTENANCE:
                    //do Maintenance stuff here
                    break;
                case UserModes.ADMINISTRATOR:
                    // do Admin stuff here
                    break;
            }
        }

Was This Post Helpful? 5
  • +
  • -

#5 MohammedA.Gouda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 12-April 11

Re: switch case and if

Posted 19 April 2011 - 01:00 AM

thx a lot

This post has been edited by Curtis Rutland: 19 April 2011 - 06:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1