Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Switch boolean.... (reinventing if, unnecessarily)

Switch boolean.... (reinventing if, unnecessarily)

Scheduled Pinned Locked Moved The Weird and The Wonderful
47 Posts 32 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Scott Corbett

    Umm...that's not true. A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.) On the other hand, if/else statements do bail as soon as the first passing conditional is found and the associated code block is executed.

    Scott E. Corbett

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #22

    Scott Corbett wrote:

    A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.)

    Not in C# - every case is required to have a terminating statement (break, goto, return or throw).

    switch (C# Reference)[^]:

    Unlike C++, C# does not allow execution to continue from one switch section to the next. ... C# requires the end of switch sections, including the final one, to be unreachable. That is, unlike some other languages, your code may not fall through into the next switch section.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    S 1 Reply Last reply
    0
    • F Freak30

      I see a reason for the function but not for the switch statement. Except if you are paid by lines of code of course. :-D

      The good thing about pessimism is, that you are always either right or pleasently surprised.

      J Offline
      J Offline
      Jeremy Falcon
      wrote on last edited by
      #23

      Totally agree.

      Jeremy Falcon

      1 Reply Last reply
      0
      • R Rob Grainger

        Came across this kind of code today...

        void EnableFromValue(bool enabled)
        {
        switch (enabled) {
        case true:
        FirstControl.Enabled = true;
        SecondControl.Enabled = true;
        ...
        break;
        case false:
        FirstControl.Enabled = false;
        SecondControl.Enabled = false;
        ...
        break;
        }
        }

        I'm sure there must be a better way ;-)

        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

        B Offline
        B Offline
        Basketcase Software
        wrote on last edited by
        #24

        Couldn't resist:

        void EnableFromValue(bool enabled)
        {
        FirstControl.Enabled = enabled;
        SecondControl.Enabled = enabled;
        ...
        }

        1 Reply Last reply
        0
        • S Scott Corbett

          Umm...that's not true. A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.) On the other hand, if/else statements do bail as soon as the first passing conditional is found and the associated code block is executed.

          Scott E. Corbett

          T Offline
          T Offline
          ttennebb
          wrote on last edited by
          #25

          I believe a switch is just a calculated jump statement. It doesn't work it's way through all the previous possibilities. Yes. Once calculated, the program goes to the break statement then jumps out appropriately. Switch statements are quite fast. In this case, I don't see advantage either way as an if statement is very simple too.

          1 Reply Last reply
          0
          • R Rob Grainger

            Came across this kind of code today...

            void EnableFromValue(bool enabled)
            {
            switch (enabled) {
            case true:
            FirstControl.Enabled = true;
            SecondControl.Enabled = true;
            ...
            break;
            case false:
            FirstControl.Enabled = false;
            SecondControl.Enabled = false;
            ...
            break;
            }
            }

            I'm sure there must be a better way ;-)

            "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

            J Offline
            J Offline
            Javier jimenez Rico
            wrote on last edited by
            #26

            it is simple : void EnableFromValue(bool ?enabled) { FirstControl.Enabled=enabled.HasValue?enabled.Value:false; SecondControl.Enabled=enabled.HasValue?enabled.Value:false; }

            M 1 Reply Last reply
            0
            • J Javier jimenez Rico

              it is simple : void EnableFromValue(bool ?enabled) { FirstControl.Enabled=enabled.HasValue?enabled.Value:false; SecondControl.Enabled=enabled.HasValue?enabled.Value:false; }

              M Offline
              M Offline
              Member 11594914
              wrote on last edited by
              #27

              This works for me FirstControl.Enabled = SecondControl.Enabled = (enabled) ? false : true;

              D 1 Reply Last reply
              0
              • F Freak30

                I see a reason for the function but not for the switch statement. Except if you are paid by lines of code of course. :-D

                The good thing about pessimism is, that you are always either right or pleasently surprised.

                B Offline
                B Offline
                Bruce Patin
                wrote on last edited by
                #28

                I have code like that. It is the "..." that is significant. For some objects, you can't just set enable = false, you have to do other things. And, in some cases, one switch branch will enable some fields and disable others.

                J 1 Reply Last reply
                0
                • S Silvabolt

                  So better way is this?

                  FirstControl.Enabled = enabled;
                  SecondControl.Enabled = enabled;

                  or better yet, MVVM would help if applicable to the app.

                  B Offline
                  B Offline
                  Bruce Patin
                  wrote on last edited by
                  #29

                  I have code like that, but sometimes it is not so simple. Not all objects have an "enabled" property, and sometimes I may need to enable some and disable others. Keeping the switch, or at least an "if ... else" structure, makes for more clarity, just in case these oddball things are necessary.

                  1 Reply Last reply
                  0
                  • M Member 11594914

                    This works for me FirstControl.Enabled = SecondControl.Enabled = (enabled) ? false : true;

                    D Offline
                    D Offline
                    DanKorn
                    wrote on last edited by
                    #30

                    LOL! I recently went through my company's entire code base to purge out constructs such as "? true : false". Even more fun were comparisons such as "if (some_int_var == TRUE)". That's great unless, say "TRUE" is defined as 1 and your variable is set to -1.

                    M 1 Reply Last reply
                    0
                    • B Bruce Patin

                      I have code like that. It is the "..." that is significant. For some objects, you can't just set enable = false, you have to do other things. And, in some cases, one switch branch will enable some fields and disable others.

                      J Offline
                      J Offline
                      jibalt
                      wrote on last edited by
                      #31

                      So do other things in the switch, but set the common values just once outside the switch ... duh. It's the DRY principle, and duplicating the code in each branch of the switch is not only stupid, but error prone.

                      T 1 Reply Last reply
                      0
                      • D DanKorn

                        LOL! I recently went through my company's entire code base to purge out constructs such as "? true : false". Even more fun were comparisons such as "if (some_int_var == TRUE)". That's great unless, say "TRUE" is defined as 1 and your variable is set to -1.

                        M Offline
                        M Offline
                        Member 11594914
                        wrote on last edited by
                        #32

                        Look. this construct is a perfect alternating on/off switch. I've used it exclusively over past 10 years. There is no valid reason not to use it, especially if you favor Clean, concise, easy to understand code. Different strokes ... Tony d

                        D 1 Reply Last reply
                        0
                        • M Member 11594914

                          Look. this construct is a perfect alternating on/off switch. I've used it exclusively over past 10 years. There is no valid reason not to use it, especially if you favor Clean, concise, easy to understand code. Different strokes ... Tony d

                          D Offline
                          D Offline
                          DanKorn
                          wrote on last edited by
                          #33

                          No, "? false : true" is still superfluous. Just use ! (the not operator). It's certainly more concise, and I would argue, even cleaner and easier to understand. FirstControl.Enabled = SecondControl.Enabled = !enabled;

                          M 1 Reply Last reply
                          0
                          • D DanKorn

                            No, "? false : true" is still superfluous. Just use ! (the not operator). It's certainly more concise, and I would argue, even cleaner and easier to understand. FirstControl.Enabled = SecondControl.Enabled = !enabled;

                            M Offline
                            M Offline
                            Member 11594914
                            wrote on last edited by
                            #34

                            Hey Dan love the use of Boolean operators. Used them extensively in the old mainframe days when storage was measured in megabytes. When working with 16mb system storage every byte counted. Here's an old trick to save bytes a *-6, ctr. Used this in 360/370 mainframes since adding the opcode (numeric value of 1) would save 1 byte. nice chatting with a knowledgeable tech

                            1 Reply Last reply
                            0
                            • S Silvabolt

                              So better way is this?

                              FirstControl.Enabled = enabled;
                              SecondControl.Enabled = enabled;

                              or better yet, MVVM would help if applicable to the app.

                              C Offline
                              C Offline
                              cramotowski
                              wrote on last edited by
                              #35

                              FirstControl.Enabled = SecondControl.Enabled = enabled;

                              1 Reply Last reply
                              0
                              • R Rob Grainger

                                Came across this kind of code today...

                                void EnableFromValue(bool enabled)
                                {
                                switch (enabled) {
                                case true:
                                FirstControl.Enabled = true;
                                SecondControl.Enabled = true;
                                ...
                                break;
                                case false:
                                FirstControl.Enabled = false;
                                SecondControl.Enabled = false;
                                ...
                                break;
                                }
                                }

                                I'm sure there must be a better way ;-)

                                "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                A Offline
                                A Offline
                                Al Chak
                                wrote on last edited by
                                #36

                                If you are sure that the function argument named enabled is bool forever? If yes than there is no reason for the switch. If chacne to change the argument type is exist so - I would add into the switch

                                default:
                                FirstControl.Enabled = enabled;
                                SecondControl.Enabled = enabled; // :laugh:

                                Richard DeemingR 1 Reply Last reply
                                0
                                • S Scott Corbett

                                  Umm...that's not true. A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.) On the other hand, if/else statements do bail as soon as the first passing conditional is found and the associated code block is executed.

                                  Scott E. Corbett

                                  S Offline
                                  S Offline
                                  Simon ORiordan from UK
                                  wrote on last edited by
                                  #37

                                  Well I kind of assumed you knew how to write a switch.

                                  1 Reply Last reply
                                  0
                                  • A Al Chak

                                    If you are sure that the function argument named enabled is bool forever? If yes than there is no reason for the switch. If chacne to change the argument type is exist so - I would add into the switch

                                    default:
                                    FirstControl.Enabled = enabled;
                                    SecondControl.Enabled = enabled; // :laugh:

                                    Richard DeemingR Offline
                                    Richard DeemingR Offline
                                    Richard Deeming
                                    wrote on last edited by
                                    #38

                                    Al Chak wrote:

                                    If chacne to change the argument type is exist so - I would add into the switch

                                    There's no reason to add a switch statement now just in case the argument type changes in six months. Add the switch when you need it - ie: when the argument type changes. :)


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                    1 Reply Last reply
                                    0
                                    • Richard DeemingR Richard Deeming

                                      Scott Corbett wrote:

                                      A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.)

                                      Not in C# - every case is required to have a terminating statement (break, goto, return or throw).

                                      switch (C# Reference)[^]:

                                      Unlike C++, C# does not allow execution to continue from one switch section to the next. ... C# requires the end of switch sections, including the final one, to be unreachable. That is, unlike some other languages, your code may not fall through into the next switch section.


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      S Offline
                                      S Offline
                                      Scott Corbett
                                      wrote on last edited by
                                      #39

                                      You're right about the switch case and fall through. Been spending too much time playing with C++ lately. My apologies.

                                      Scott E. Corbett

                                      1 Reply Last reply
                                      0
                                      • R Rob Grainger

                                        Came across this kind of code today...

                                        void EnableFromValue(bool enabled)
                                        {
                                        switch (enabled) {
                                        case true:
                                        FirstControl.Enabled = true;
                                        SecondControl.Enabled = true;
                                        ...
                                        break;
                                        case false:
                                        FirstControl.Enabled = false;
                                        SecondControl.Enabled = false;
                                        ...
                                        break;
                                        }
                                        }

                                        I'm sure there must be a better way ;-)

                                        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                        C Offline
                                        C Offline
                                        chaosworrier_oz
                                        wrote on last edited by
                                        #40

                                        If it is not break-en, don't switch it? [[Thank you, I'm here all week; try the veal...]] Chaos.

                                        B 1 Reply Last reply
                                        0
                                        • R Rob Grainger

                                          Came across this kind of code today...

                                          void EnableFromValue(bool enabled)
                                          {
                                          switch (enabled) {
                                          case true:
                                          FirstControl.Enabled = true;
                                          SecondControl.Enabled = true;
                                          ...
                                          break;
                                          case false:
                                          FirstControl.Enabled = false;
                                          SecondControl.Enabled = false;
                                          ...
                                          break;
                                          }
                                          }

                                          I'm sure there must be a better way ;-)

                                          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                          T Offline
                                          T Offline
                                          TheGreatAndPowerfulOz
                                          wrote on last edited by
                                          #41

                                          well, switch is shorthand for the if--then--else construct.

                                          #SupportHeForShe

                                          If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups