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. The Lounge
  3. Visual Basic 6 did this, why can't you, C#?

Visual Basic 6 did this, why can't you, C#?

Scheduled Pinned Locked Moved The Lounge
csharpc++helpquestion
27 Posts 17 Posters 7 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.
  • D Offline
    D Offline
    den2k88
    wrote on last edited by
    #1

    Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

    GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

    R OriginalGriffO L P T 11 Replies Last reply
    0
    • D den2k88

      Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

      GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      Probably because it was modelled on languages which do allow execution to fall-through, and without an explicit break / return, early adopters might have thought the C# code was falling through. [Proposal] Case fall through should be allowed · dotnet/csharplang · Discussion #603 · GitHub[^] See also Eric Lippert's comments on SO:

      Eric Lippert wrote:[^]:

      C# enforces the no-fall-through rule in every switch section, including the last one. This is so that switch sections can be re-ordered arbitrarily, possibly by mechanical tools, without introducing semantic changes in the program. C# enforces the no-fall-through rule by requiring that the end point of every switch section be unreachable. It is not necessary for a switch section to end in a break. It can end in a break, return, goto, continue, throw, or a detectable infinite loop:


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

      D R M 3 Replies Last reply
      0
      • R Richard Deeming

        Probably because it was modelled on languages which do allow execution to fall-through, and without an explicit break / return, early adopters might have thought the C# code was falling through. [Proposal] Case fall through should be allowed · dotnet/csharplang · Discussion #603 · GitHub[^] See also Eric Lippert's comments on SO:

        Eric Lippert wrote:[^]:

        C# enforces the no-fall-through rule in every switch section, including the last one. This is so that switch sections can be re-ordered arbitrarily, possibly by mechanical tools, without introducing semantic changes in the program. C# enforces the no-fall-through rule by requiring that the end point of every switch section be unreachable. It is not necessary for a switch section to end in a break. It can end in a break, return, goto, continue, throw, or a detectable infinite loop:


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

        D Offline
        D Offline
        den2k88
        wrote on last edited by
        #3

        For the most common scenario it just adds a line of code that has to be written which has no meaning. It just clutters the code with additional meaningless lines. Bah, or better, :baaaa!:!

        GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

        1 Reply Last reply
        0
        • D den2k88

          Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

          GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          I agree with a specific "end of case" being required - as Richard quoted, it allows the compiler to spot code faults instead of letting them cause runtime problems. It may be "unnecessary", but it does trap some errors which could be difficult to diagnose at run time.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • D den2k88

            Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

            GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

            L Offline
            L Offline
            lmoelleb
            wrote on last edited by
            #5

            It does allow multiple "entry points", so case 1: case 2: Some code break; So it can't just assume a break. Sure they can come up with rules for how it would be interpreted, but kind of glad they didn't.

            OriginalGriffO D 2 Replies Last reply
            0
            • D den2k88

              Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

              GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I blame Ken Thompson. switch should never have had fall-through, break should never have affected switch. That should have been their first clue.

              O 1 Reply Last reply
              0
              • P PIEBALDconsult

                I blame Ken Thompson. switch should never have had fall-through, break should never have affected switch. That should have been their first clue.

                O Offline
                O Offline
                obermd
                wrote on last edited by
                #7

                I blame Ken Thompson for the vast majority of bugs in today's software. The entire concept of null terminated strings and buffers is simply asking for trouble. DEC and IBM both had descriptor-based buffers, which gives the OS calls that manipulate buffers a way to block buffer overflows.

                D E 2 Replies Last reply
                0
                • D den2k88

                  Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

                  GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                  T Offline
                  T Offline
                  tronderen
                  wrote on last edited by
                  #8

                  Default fallthrough is one of the major reasons why C is called a machine independent assembler. Or of you like: A yafi-ygi language.

                  Religious freedom is the freedom to say that two plus two make five.

                  D 1 Reply Last reply
                  0
                  • D den2k88

                    Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

                    GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                    T Offline
                    T Offline
                    tronderen
                    wrote on last edited by
                    #9

                    I will propose a related extension to C#: Default fall through to the next method. If the flow runs out of a method body's closing brace, with no explicit return statement, execution continues to the following method. This would be very useful e.g. for methods that take arguments of two different kinds, say integer and string: The first method would accept, say, a numeric string and convert it to a binary integer, then flowing on to the following method doing the processing on binary integers, regardless of which entry point was used. Fortran has (or had?) its ENTRY statement providing this kind of functionality. While multiple entry points can be simulated by the first method explicitly calling the other, after format conversion, this adds a completely unnecessary statement, as well as a handful unnecessary instruction for the call management, and increased stack demands. I cannot think of any single use case were switch case fall through is well justified, and where the arguments for it can not reasonably be extended to justify method fall through. If one makes sense, so does the other.

                    Religious freedom is the freedom to say that two plus two make five.

                    G 1 Reply Last reply
                    0
                    • R Richard Deeming

                      Probably because it was modelled on languages which do allow execution to fall-through, and without an explicit break / return, early adopters might have thought the C# code was falling through. [Proposal] Case fall through should be allowed · dotnet/csharplang · Discussion #603 · GitHub[^] See also Eric Lippert's comments on SO:

                      Eric Lippert wrote:[^]:

                      C# enforces the no-fall-through rule in every switch section, including the last one. This is so that switch sections can be re-ordered arbitrarily, possibly by mechanical tools, without introducing semantic changes in the program. C# enforces the no-fall-through rule by requiring that the end point of every switch section be unreachable. It is not necessary for a switch section to end in a break. It can end in a break, return, goto, continue, throw, or a detectable infinite loop:


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

                      R Offline
                      R Offline
                      Rob Philpott
                      wrote on last edited by
                      #10

                      Richard Deeming wrote:

                      detectable infinite loop

                      Classic! You won't find any of my switch statements without one of those. :)

                      Regards, Rob Philpott.

                      1 Reply Last reply
                      0
                      • L lmoelleb

                        It does allow multiple "entry points", so case 1: case 2: Some code break; So it can't just assume a break. Sure they can come up with rules for how it would be interpreted, but kind of glad they didn't.

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        Personally, I'd have preferred multiple entries to be a different syntax:

                        case 1,
                        2,
                        3:
                        ... code
                        break;
                        case 4:
                        ... code
                        break;

                        Or even:

                        case 1,
                        2,
                        3:
                        {
                        ... code
                        break;
                        }
                        case 4:
                        {
                        ... code
                        break;
                        }

                        I think it would have been clearer as well as less easy to mess up by adding a new case between them. Perhaps with brackets round the case list as well?

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        T 1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Personally, I'd have preferred multiple entries to be a different syntax:

                          case 1,
                          2,
                          3:
                          ... code
                          break;
                          case 4:
                          ... code
                          break;

                          Or even:

                          case 1,
                          2,
                          3:
                          {
                          ... code
                          break;
                          }
                          case 4:
                          {
                          ... code
                          break;
                          }

                          I think it would have been clearer as well as less easy to mess up by adding a new case between them. Perhaps with brackets round the case list as well?

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                          T Offline
                          T Offline
                          tronderen
                          wrote on last edited by
                          #12

                          OriginalGriff wrote:

                          Or even:

                          case 1,
                          2,
                          3:
                          {
                          ... code
                          break;
                          }
                          case 4:
                          {
                          ... code
                          break;
                          }

                          The Pascal style (except for the 'break' statements). I don't like the C# 'No fallthrough except when the case alternative is empty', so I sort of agree with you, although it is not a big matter. I do upset some people by always using braces, like in your second alternative: Indentation and braces go hand-in-hand. Call it 'two-factor level nesting'. I want to indent the case alternative, so it should be made a block. Blocks are indented. Statements are not, neither single nor multiple statements. In- and un-denting without braces makes a mess of nesting levels.

                          Religious freedom is the freedom to say that two plus two make five.

                          R 1 Reply Last reply
                          0
                          • L lmoelleb

                            It does allow multiple "entry points", so case 1: case 2: Some code break; So it can't just assume a break. Sure they can come up with rules for how it would be interpreted, but kind of glad they didn't.

                            D Offline
                            D Offline
                            den2k88
                            wrote on last edited by
                            #13

                            I didn't know this (C# is not my main language) and now it makes sense. From the compiler errors I supposed it didn't allow multiple entry points as well, just like VB6 used to do.

                            GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                            1 Reply Last reply
                            0
                            • O obermd

                              I blame Ken Thompson for the vast majority of bugs in today's software. The entire concept of null terminated strings and buffers is simply asking for trouble. DEC and IBM both had descriptor-based buffers, which gives the OS calls that manipulate buffers a way to block buffer overflows.

                              D Offline
                              D Offline
                              den2k88
                              wrote on last edited by
                              #14

                              Nice, but... which OS? There are no OS where we are going.

                              GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                              1 Reply Last reply
                              0
                              • T tronderen

                                Default fallthrough is one of the major reasons why C is called a machine independent assembler. Or of you like: A yafi-ygi language.

                                Religious freedom is the freedom to say that two plus two make five.

                                D Offline
                                D Offline
                                den2k88
                                wrote on last edited by
                                #15

                                trønderen wrote:

                                C is called a machine independent assembler.

                                That's why I love it

                                GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                                1 Reply Last reply
                                0
                                • D den2k88

                                  Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

                                  GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                                  M Offline
                                  M Offline
                                  maze3
                                  wrote on last edited by
                                  #16

                                  yes, but the "new" switch expression 🤤

                                  1 Reply Last reply
                                  0
                                  • D den2k88

                                    Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

                                    GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                                    S Offline
                                    S Offline
                                    Stacy Dudovitz
                                    wrote on last edited by
                                    #17

                                    Allowing fall through code was/is a huge source of bugs, and does not fall in line with best practices. During any refactoring, it's not uncommon to move/reorder code. Without close scrutiny, a break in logic flow would be so easily introduced by missing the fall through code. Worse, refactoring/reordering code might be necessary, but now fallthrough code has introduced an artificial constraint. For case statements with few case points, this requirement is a minor inconvenience at best. If, however, you have numerous case points, this might be indicative of a code smell. Such code is both difficult to understand and to follow. Perhaps a different approach might be more appropriate... fi ite state machines, keyword/lambda method dictionaries, etc. Something to consider when designing well crafted code that adheres to best practices.

                                    1 Reply Last reply
                                    0
                                    • T tronderen

                                      I will propose a related extension to C#: Default fall through to the next method. If the flow runs out of a method body's closing brace, with no explicit return statement, execution continues to the following method. This would be very useful e.g. for methods that take arguments of two different kinds, say integer and string: The first method would accept, say, a numeric string and convert it to a binary integer, then flowing on to the following method doing the processing on binary integers, regardless of which entry point was used. Fortran has (or had?) its ENTRY statement providing this kind of functionality. While multiple entry points can be simulated by the first method explicitly calling the other, after format conversion, this adds a completely unnecessary statement, as well as a handful unnecessary instruction for the call management, and increased stack demands. I cannot think of any single use case were switch case fall through is well justified, and where the arguments for it can not reasonably be extended to justify method fall through. If one makes sense, so does the other.

                                      Religious freedom is the freedom to say that two plus two make five.

                                      G Offline
                                      G Offline
                                      gggustafson
                                      wrote on last edited by
                                      #18

                                      int method ( int integer )
                                      {

                                      return ( 42 );
                                      
                                      }
                                      

                                      int method ( string trigger )
                                      {

                                      return ( method ( Int32.Parse ( trigger ) ) );
                                      
                                      }
                                      

                                      Gus Gustafson

                                      1 Reply Last reply
                                      0
                                      • D den2k88

                                        Small rant about dumb choices. So C# doesn't allow cascading through switch cases. Ok, understandable. So why the :elephant: does it need me to repeat the :elephant:ing break; after each and any case? There is no :elephant:ing need for it because there is nothing to break, apart from my patience. Rant over.

                                        GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X The shortest horror story: On Error Resume Next

                                        Z Offline
                                        Z Offline
                                        zezba9000
                                        wrote on last edited by
                                        #19

                                        That isn't true.

                                        switch (value)
                                        {
                                        case 0:
                                        case 1:
                                        case 2:
                                        // do something
                                        break;
                                        }

                                        1 Reply Last reply
                                        0
                                        • T tronderen

                                          OriginalGriff wrote:

                                          Or even:

                                          case 1,
                                          2,
                                          3:
                                          {
                                          ... code
                                          break;
                                          }
                                          case 4:
                                          {
                                          ... code
                                          break;
                                          }

                                          The Pascal style (except for the 'break' statements). I don't like the C# 'No fallthrough except when the case alternative is empty', so I sort of agree with you, although it is not a big matter. I do upset some people by always using braces, like in your second alternative: Indentation and braces go hand-in-hand. Call it 'two-factor level nesting'. I want to indent the case alternative, so it should be made a block. Blocks are indented. Statements are not, neither single nor multiple statements. In- and un-denting without braces makes a mess of nesting levels.

                                          Religious freedom is the freedom to say that two plus two make five.

                                          R Offline
                                          R Offline
                                          realJSOP
                                          wrote on last edited by
                                          #20

                                          You only need braces in the case when you're creating a new variable in the case. Otherwise, they're unnecessary.

                                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                          -----
                                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                          -----
                                          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                                          K 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