Re: Can someone explain why switch syntax is the way it is?




"Scott Roberts" <scott.roberts@xxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:OX%23iVHA0FHA.460@xxxxxxxxxxxxxxxxxxxxxxx
>
> "Andrew Ducker" <andrew@xxxxxxxxxxxxx> wrote in message
> news:1129209462.752861.88480@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>> And no, this isn't a complaint about break - I'm very happy to make
>> things explicit.
>>
>> However, why isn't the format something like:
>>
>> switch(myVariable)
>> {
>> case 1:
>> {
>> //Do Something
>> }
>> case 2:
>> {
>> //Do Something Else
>> }
>> default
>> // Do Nothing at all
>> }
>>
>> That way the 'break' is subsumed into the closing curly brackets, and
>> it follows the same way of grouping commands as the rest of the
>> language does.
>>
>> Andy D
>
> Well, it would certainly be more "C# like" that way, but it would prevent
> intentional "flow through" from one case to another. While I think such
> "flow through" is a horrible, horrible idea, I'm sure someone out there
> thinks it's absolutely necessary.
>
>

Since C# does not support the C++ style of "flow through" by leaving out the
break statement between cases (except when multiple cases pertain to the
exact same block of code), this is really not a big problem. Something like
this would work:

switch(val)
{
case 1:
case 2:
{
textBox1.Text = "Nothing";
}
case 3:
{
textBox1.Text = "Answer";
}
default:
{
}
}

I like it. It is definitely more consistent. Having this style as an
alternative format really does not seem to add any confusion - it is just as
clear what is being done.


.



Relevant Pages

  • Re: Can someone explain why switch syntax is the way it is?
    ... this isn't a complaint about break - I'm very happy to make ... > things explicit. ... why isn't the format something like: ... Simply exiting a switch statement on reaching the end ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can someone explain why switch syntax is the way it is?
    ... this isn't a complaint about break - I'm very happy to make ... > things explicit. ... why isn't the format something like: ... intentional "flow through" from one case to another. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: style issue
    ... This message is posted to a newsgroup. ... >> I have a style called "Flow" which is used when I list steps in a flow. ... So I have typed the first paragraph in the ... >> So I go to Format, Styles and Formatting and modify the style. ...
    (microsoft.public.word.pagelayout)
  • Re: format
    ... how to create a document with 2 columns in a page, the text will flow ... In Word 2003 or earlier, click Format> Columns, choose two columns, ... click the More Columns item at the bottom of the menu. ... Jay Freedman ...
    (microsoft.public.word.docmanagement)
  • Re: style issue
    ... is the setting of the WordPerfect options on the General tab of Tools | ... > I have a style called "Flow" which is used when I list steps in a flow. ... So I have typed the first paragraph in the list. ... > So I go to Format, Styles and Formatting and modify the style. ...
    (microsoft.public.word.pagelayout)