switch statement


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: switch statement

Hybrid View

  1. #1
    James Lin Guest

    switch statement

    If you look at the preliminary documentation released at the end of June in
    the MSDN library, you'll notice a little blurb on the switch statement. The
    document says that there are no "fall-throughs" between each case, but a
    break is still required to exit the switch statement. See

    http://msdn.microsoft.com/library/pr...spec_8_7_2.htm

    But in the September issue of MSDN Magazine, there is an article on C# that
    states that there ARE implied break statements between each case. See

    http://msdn.microsoft.com/msdnmag/is...arp/csharp.asp

    Does this indicate that the design of C#, VB.NET, and .NET in general is in
    more flux than we realize? (Or that the author of the magazine article is
    simply wrong? 8) )

    Thanks,
    - Jim
    --
    James Lin
    jlin@ugcs.caltech.edu

    http://www.ugcs.caltech.edu/~jlin/




  2. #2
    Robert Scoble Guest

    Re: switch statement

    Keep in mind that anything you read in a magazine is at least four weeks old
    (and is probably older than that).

    So, just by age alone I would go with the MSDN library's information.

    Robert Scoble
    http://conferences.devx.com

    ###


    "James Lin" <jlin@ugcs.caltech.edu> wrote in message
    news:397a5823$1@news.devx.com...
    > If you look at the preliminary documentation released at the end of June

    in
    > the MSDN library, you'll notice a little blurb on the switch statement.

    The
    > document says that there are no "fall-throughs" between each case, but a
    > break is still required to exit the switch statement. See
    >
    > http://msdn.microsoft.com/library/pr...spec_8_7_2.htm
    >
    > But in the September issue of MSDN Magazine, there is an article on C#

    that
    > states that there ARE implied break statements between each case. See
    >
    > http://msdn.microsoft.com/msdnmag/is...arp/csharp.asp
    >
    > Does this indicate that the design of C#, VB.NET, and .NET in general is

    in
    > more flux than we realize? (Or that the author of the magazine article is
    > simply wrong? 8) )
    >
    > Thanks,
    > - Jim
    > --
    > James Lin
    > jlin@ugcs.caltech.edu
    >
    > http://www.ugcs.caltech.edu/~jlin/
    >
    >
    >




  3. #3
    James Curran Guest

    Re: switch statement

    "James Lin" <jlin@ugcs.caltech.edu> wrote in message
    news:397a5823$1@news.devx.com...
    > Does this indicate that the design of C#, VB.NET, and .NET in general is

    in
    > more flux than we realize? (Or that the author of the magazine article is
    > simply wrong? 8) )


    The MSDN magazine technically isn't wrong, since it never said that
    breaks weren't needed. My guess is the either the author glossed over that
    point, in an attempt to hit as many points as possible in a brief article,
    or wrote the article based on sketchier internal documentation, which itself
    did not make the point as clearly as the later version.

    --
    Truth,
    James Curran
    http://www.NJTheater.com (Professional)
    http://www.NovelTheory.com (Personal)
    http://www.BrandsForLess.com (Day Job)




  4. #4
    James Lin Guest

    Re: switch statement

    "James Curran" <jamescurran@mvps.org> wrote in message
    news:397dd23b$1@news.devx.com...
    > The MSDN magazine technically isn't wrong, since it never said that
    > breaks weren't needed.


    Yes, it did:

    * * *

    Another programmer-friendly feature is the improvement over C++ in the way
    switch statements work. In C++, you could write a switch statement that fell
    through from case to case. For example, this code

    switch (i)
    {
    case 1:
    FunctionA();

    case 2:
    FunctionB();
    Break;
    }

    would call both FunctionA and FunctionB if i was equal to 1. C# works like
    Visual Basic, putting an implied break before each case statement.

    * * *

    > My guess is the either the author glossed over that point, in an attempt
    > to hit as many points as possible in a brief article, or wrote the article
    > based on sketchier internal documentation, which itself did not make the
    > point as clearly as the later version.


    That's what I'm thinking too. I understand that details are subject to
    change at this early stage.

    - Jim
    --
    James Lin
    jlin@ugcs.caltech.edu

    http://www.ugcs.caltech.edu/~jlin/




  5. #5
    point Guest

    Re: switch statement

    That is *not* programmer friendly,
    that is programmer 'idiot proofing'.

    Programmer friendly gives the programmer more control not
    less.

    besides, MS states that the C# reference is not final,
    *not final* is usually implied with any MS product
    so if they are explicit about *not final* then you can
    bet it will change drastically.


    James Lin wrote in message <397e9012$3@news.devx.com>...
    >"James Curran" <jamescurran@mvps.org> wrote in message
    >news:397dd23b$1@news.devx.com...
    >> The MSDN magazine technically isn't wrong, since it never

    said that
    >> breaks weren't needed.

    >
    > Yes, it did:
    > ***
    > Another programmer-friendly feature is the improvement

    over C++ in the way
    > switch statements work. In C++, you could write a switch

    statement that fell
    > through from case to case. For example, this code
    >
    > switch (i)
    > {
    > case 1:
    > FunctionA();
    >
    > case 2:
    > FunctionB();
    > Break;
    > }
    >
    > would call both FunctionA and FunctionB if i was equal to

    1. C# works like
    > Visual Basic, putting an implied break before each case

    statement.
    >





  6. #6
    Eric Gunnerson Guest

    Re: switch statement

    C# does not look like VB. When you write some code as part of a case,
    control cannot fall off the end of the case and into the next case; it must
    go somewhere else, via a break, return, goto, or thrown exception.

    In other words, it's an error to write something like:

    string s = "hello";

    switch (s)
    {
    case "hello":
    case "goodbye":
    // processing here;
    // no break;

    case "how are you":
    // more processing here.
    break;
    }

    There must be a break on the "no break" line. If you want to have
    fall-through, you must make it explicit with a goto case.


    "James Lin" <jlin@ugcs.caltech.edu> wrote in message
    news:397e9012$3@news.devx.com...
    > "James Curran" <jamescurran@mvps.org> wrote in message
    > news:397dd23b$1@news.devx.com...
    > > The MSDN magazine technically isn't wrong, since it never said that
    > > breaks weren't needed.

    >
    > Yes, it did:
    >
    > * * *
    >
    > Another programmer-friendly feature is the improvement over C++ in the way
    > switch statements work. In C++, you could write a switch statement that

    fell
    > through from case to case. For example, this code
    >
    > switch (i)
    > {
    > case 1:
    > FunctionA();
    >
    > case 2:
    > FunctionB();
    > Break;
    > }
    >
    > would call both FunctionA and FunctionB if i was equal to 1. C# works like
    > Visual Basic, putting an implied break before each case statement.
    >
    > * * *
    >
    > > My guess is the either the author glossed over that point, in an attempt
    > > to hit as many points as possible in a brief article, or wrote the

    article
    > > based on sketchier internal documentation, which itself did not make the
    > > point as clearly as the later version.

    >
    > That's what I'm thinking too. I understand that details are subject to
    > change at this early stage.
    >
    > - Jim
    > --
    > James Lin
    > jlin@ugcs.caltech.edu
    >
    > http://www.ugcs.caltech.edu/~jlin/
    >
    >
    >




  7. #7
    Paul Vick [MSFT] Guest

    Re: switch statement

    Actually, this is very much like VB, just that VB doesn't require the break.
    There is no fallthrough in VB select case statements.

    Paul

    "Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
    news:398f14b3$1@news.devx.com...
    > C# does not look like VB. When you write some code as part of a case,
    > control cannot fall off the end of the case and into the next case; it

    must
    > go somewhere else, via a break, return, goto, or thrown exception.
    >
    > In other words, it's an error to write something like:
    >
    > string s = "hello";
    >
    > switch (s)
    > {
    > case "hello":
    > case "goodbye":
    > // processing here;
    > // no break;
    >
    > case "how are you":
    > // more processing here.
    > break;
    > }
    >
    > There must be a break on the "no break" line. If you want to have
    > fall-through, you must make it explicit with a goto case.
    >
    >
    > "James Lin" <jlin@ugcs.caltech.edu> wrote in message
    > news:397e9012$3@news.devx.com...
    > > "James Curran" <jamescurran@mvps.org> wrote in message
    > > news:397dd23b$1@news.devx.com...
    > > > The MSDN magazine technically isn't wrong, since it never said that
    > > > breaks weren't needed.

    > >
    > > Yes, it did:
    > >
    > > * * *
    > >
    > > Another programmer-friendly feature is the improvement over C++ in the

    way
    > > switch statements work. In C++, you could write a switch statement that

    > fell
    > > through from case to case. For example, this code
    > >
    > > switch (i)
    > > {
    > > case 1:
    > > FunctionA();
    > >
    > > case 2:
    > > FunctionB();
    > > Break;
    > > }
    > >
    > > would call both FunctionA and FunctionB if i was equal to 1. C# works

    like
    > > Visual Basic, putting an implied break before each case statement.
    > >
    > > * * *
    > >
    > > > My guess is the either the author glossed over that point, in an

    attempt
    > > > to hit as many points as possible in a brief article, or wrote the

    > article
    > > > based on sketchier internal documentation, which itself did not make

    the
    > > > point as clearly as the later version.

    > >
    > > That's what I'm thinking too. I understand that details are subject to
    > > change at this early stage.
    > >
    > > - Jim
    > > --
    > > James Lin
    > > jlin@ugcs.caltech.edu
    > >
    > > http://www.ugcs.caltech.edu/~jlin/
    > >
    > >
    > >

    >
    >




  8. #8
    Matt Searle Guest

    Re: switch statement

    How about introducing a "CarryOnMate" command if you don't want it to
    break - Or I remember a "Continue" command from somewhere.

    Matt#
    http://csharpindex.com

    "Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
    news:398f14b3$1@news.devx.com...
    > C# does not look like VB. When you write some code as part of a case,
    > control cannot fall off the end of the case and into the next case; it

    must
    > go somewhere else, via a break, return, goto, or thrown exception.
    >
    > In other words, it's an error to write something like:
    >
    > string s = "hello";
    >
    > switch (s)
    > {
    > case "hello":
    > case "goodbye":
    > // processing here;
    > // no break;
    >
    > case "how are you":
    > // more processing here.
    > break;
    > }
    > <snip>




  9. #9
    Joe Mayo Guest

    Re: switch statement

    I know I'm going to catch it for even suggesting this, but there is a way to
    make a C# switch act like a C/C++ switch:

    switch (count) {
    case 0:
    // do zero stuff
    goto one;
    case 1:
    one:
    // do one stuff
    goto two;
    case 2:
    two:
    // do two stuff
    break;
    default:
    // do default stuff
    break;
    }

    --
    Joe Mayo
    C# Station - Information, Links, and Other
    Resources for the C# Programming Language
    http://www.csharp-station.com/


    "Matt Searle" <matt@8001dotcom> wrote in message
    news:3997fdf2@news.devx.com...
    > How about introducing a "CarryOnMate" command if you don't want it to
    > break - Or I remember a "Continue" command from somewhere.
    >
    > Matt#
    > http://csharpindex.com
    >
    > "Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
    > news:398f14b3$1@news.devx.com...
    > > C# does not look like VB. When you write some code as part of a case,
    > > control cannot fall off the end of the case and into the next case; it

    > must
    > > go somewhere else, via a break, return, goto, or thrown exception.
    > >
    > > In other words, it's an error to write something like:
    > >
    > > string s = "hello";
    > >
    > > switch (s)
    > > {
    > > case "hello":
    > > case "goodbye":
    > > // processing here;
    > > // no break;
    > >
    > > case "how are you":
    > > // more processing here.
    > > break;
    > > }
    > > <snip>

    >
    >




  10. #10
    ralph Guest

    Re: switch statement


    Hey! I know you.

    You are the guy that was holding the "NMAKE RULES!" sign when the IDE Convention
    bus rolled by.

    I love it. <smile>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center