Groups | Blog | Home
all groups > c# > august 2003 >

c# : C# Switch statement


Harry Bosch
8/17/2003 1:31:33 PM
[quoted text, click to view]

You should get a beginner's C# book and start reading it.

--
Duncan McNutt [BSDM]
8/17/2003 10:14:21 PM
Is it possible to have ranges or patterns in a case ?

If not, why wasnt this designed in to make it easier instead of listing
every case value needed?

--

Duncan McNutt
Microsoft Product Deactivation Team
--



Duncan McNutt .[BSDM]
8/17/2003 10:39:39 PM
I am trying to install MIS 2002 Trial I downloaded from the MS site but I
get the following errors.

How can I fix this and get it installing? Any ideas?

Event Type: Error
Event Source: MsiInstaller
Event Category: None
Event ID: 11720
Date: 8/17/2003
Time: 10:09:38 PM
User: N/A
Computer: VMWARE2KPRO
Description:
Product: Mobile Information Server -- Error 1720.There is a problem with
this Windows Installer package. A script required for this install to
complete could not be run. Contact your support personnel or package vendor.
Custom action IsDCLogon script error -2147023665, : Line 2339, Column 5,

--

Duncan McNutt
Microsoft Product Deactivation Team
--



[quoted text, click to view]

Duncan McNutt .[BSDM]
8/17/2003 10:42:50 PM
oops, wrong thread.

--

Duncan McNutt
Microsoft Product Deactivation Team
--



[quoted text, click to view]

Edward Yang
8/18/2003 9:21:34 AM
I love C#. But I strongly disagree with M$ C# team on the design of the
switch-break statement.

C# does not allow automatic fall through for each case statement, yet it
requires a mandatory and useless break statement. It's completely insane to
have such a design. The break statement is compeletely NOT NECESSARY!

I understand they want to make programmers make LESS errors by breaking
C/C++ goodness (e.g., range of values). But those errors caused by
case-break pair never get in my way throughout 8 years of C/C++ programming
experience. Actually I've never made a mistake when coding case-break's.

I think they might want to give a compiler warning when there is no break
statement after a case statement. That's the best way keeping C/C++ goodness
yet giving programmers much, much less chance for making errors.

Edward


[quoted text, click to view]

Edward Yang
8/18/2003 9:42:55 AM
OMG, I thought it is not allowed to do this!

I read MSDN again, and find this:

Although fall through from one case label to another is not supported, it is
allowed to stack case labels, for example:
case 0:
case 1:
// do something;
Edward

[quoted text, click to view]

Edward Yang
8/18/2003 9:45:13 AM
Sorry, I want to correct a small part of my comment. See Stan's reply in
this thread above this post.

C# does allow the old way of 'stacking' case's.

<msdn>
Although fall through from one case label to another is not supported, it is
allowed to stack case labels, for example:
case 0:
case 1:
// do something; </msdn>

But do not put any code (comment is OK) after case 0 or case 1, otherwise
the non-fall-through compiler error will get you.

Edward

[quoted text, click to view]

Stan
8/18/2003 10:14:35 AM
Here, I did a search and copied one of the many replies to this common
question.
[quoted text, click to view]

Yes, like this:

using System;

class Test
{

public static void Main()
{
int x;
x=Convert.ToInt16(Console.ReadLine());
switch (x)
{
case 1:
case 2:
case 3:
Console.WriteLine("Is 1 2 3");
break;
case 4:
case 5:
case 6:
Console.WriteLine("Is 4 5 6");
break;
default:
Console.WriteLine("Is greater than 6");
break;
}
}
}

However, you can't do greater than/less than (i.e. case 4 to 6) like
you could do in VB. You'll have to use if/else statements for that.

'Lib



[quoted text, click to view]

Eric Gunnerson [MS]
8/18/2003 1:23:33 PM
It's a feature vs. complexity thing.. While we agree that there are cases
where case ranges are useful, we don't think that their usefullness above
what you get with a simple series of if statements outweighs the increase in
complexity.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
[quoted text, click to view]

Michael Culley
8/18/2003 1:26:09 PM
[quoted text, click to view]

Bullshit.

--
Michael Culley

Austin Ehlers
8/18/2003 9:53:47 PM
On Mon, 18 Aug 2003 13:23:33 -0700, "Eric Gunnerson [MS]"
[quoted text, click to view]

I agree. However, what about adding syntax like this:

switch (num)
{
case 1 to 5:
//code
break;
}

and translate it to the appropriate IL as if it were written like:

switch (num)
{
case 1:
case 2:
case 3:
case 4:
case 5:
//code
break;
}

As this allows increased readability and is easily maintainable?

Thanks,
Duncan .McNutt
8/18/2003 10:35:55 PM
Complexity for whom?

--

Duncan McNutt
Microsoft Product Deactivation Team
--


[quoted text, click to view]

Duncan McNutt [FTSE].
8/18/2003 11:24:55 PM
So a little thing like that that may aid code readability is not good but
adding crap like Lambada functions (anonymous methods) and partial types (
to aid splitting source files) that is good?

You have the BCLs make an entire pigs mess of VSplits and HSplits and you
say thats good?

Yet adding a simple thing like ranges into a case in the switch construct is
bad because you cant be assed to handle the complexity?

Pass the bong.


--

Duncan McNutt
Microsoft Product Deactivation Team
--


[quoted text, click to view]

Duncan McNutt [FTSE]_
8/19/2003 10:07:48 AM
That is complex??


But theyre adding Lambada funtions (anonymous methods) and Partial types!!!

Thats even MORE complex and POINTLESS !!!

--

Duncan McNutt
Microsoft Product Deactivation Team
--


[quoted text, click to view]

craig
8/19/2003 11:07:49 AM
Amen.

[quoted text, click to view]

Eric Gunnerson [MS]
8/19/2003 1:56:16 PM
Both partial types and anonymous methods allow us to deal with situations
where C# does not work very well, and therefore the benefit that they bring
is higher than the benefit that case ranges would bring.

Partial types will let us segregate Windows Forms code into
designer-generated code and user code. That will make the model much easier.
There are other scenarios, but that's the primary one.

Anonymous methods are very useful in a case where you need a small bit of
code to pass as a delegate. If you put it in a separate method, you end up
with a disconnect between the small bit of code and where it's actually
used, which makes it harder to understand what's going on. If used for
scenarios such as this, anonymous methods will make your code more readable.

There is the chance for abuse with both features.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
[quoted text, click to view]

Eric Gunnerson [MS]
8/19/2003 2:00:06 PM
I'd have to see some actual code to understand how common this was and
whether it made sense to have a syntax for it. If it were only cases 1 - 5,
you'd obviously write it with a single-clause if. If there are a lot more
cases, I might be persuaded that it would be clearer to have a range. It
would take a fair bit of data to understand what people were really doing
and what the result of a new feature would be.

My experience is that switch statements are fairly rare in OO code.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
[quoted text, click to view]

AddThis Social Bookmark Button