Control cannot fall through from one case label ('case 115:')

P

puzzlecracker

I just got this error, without more dissection it appears to be
preposterous for csharp to prevent cases in the switch to fall
through. In my case, I need to be able to fall through as the action
takes is the same for those cases.



Is that a joke?

switch(value)
{
case 1:
case2:
//do something
break;
default:
break
}

why should this fail?
 
J

Jason Newell

Should work fine if you don't have any typeos.

- Case 2 didn't have a space between the words
- No semicolon after the last break

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

Jason Newell
www.jasonnewell.net
 
G

Göran Andersson

Peter said:
You have to write an explicit "goto". In C#, every case must end with
_something_; implicit fall-through is not allowed.

With the exception of empty cases, of course.
 
B

Bill Butler

puzzlecracker said:
I just got this error, without more dissection it appears to be
preposterous for csharp to prevent cases in the switch to fall
through. In my case, I need to be able to fall through as the action
takes is the same for those cases.



Is that a joke?

switch(value)
{
case 1:
case2:
//do something
break;
default:
break
}

why should this fail?

As long as you have no code between the two cases it will fall through
e.g. (this works)
case 0:
case 4: Console.WriteLine("Foo"); break;

but this will not compile without a break (or goto)
case 0: Console.WriteLine("Foo");
case 4: Console.WriteLine("Bar"); break;

Your sample should work (once the syntax errors are fixed)

Bill
 
P

puzzlecracker

As long as you have no code between the two cases it will fall through
e.g. (this works)
    case 0:
    case 4:  Console.WriteLine("Foo"); break;

but this will not compile without a break (or goto)
    case 0:  Console.WriteLine("Foo");
    case 4:  Console.WriteLine("Bar"); break;

Your sample should work (once the syntax errors are fixed)

    Bill

Learn something new everyday.... This sort of thinks, yes, prevent a
lot of mistakes, yet makes us stupid!!!
 
R

Rudy Velthuis

Peter said:
You have to write an explicit "goto". In C#, every case must end
with something; implicit fall-through is not allowed.

This language feature is one of many designed to prevent some of the
most common typographical bugs that show up in other languages.

They could have done it the Pascal way. No problems with fall-through
or common typo bugs there. <g>
 
P

Peter Morris

They could have done it the Pascal way. No problems with fall-through
or common typo bugs there. <g>

Yes they could have. But then again, having to mangle your source because
you can't do a switch on a string wouldn't have been fun would it?
 
R

Rudy Velthuis

Peter said:
Yes they could have. But then again, having to mangle your source
because you can't do a switch on a string wouldn't have been fun
would it?

They could have done that for strings as well, no biggie. <g>

I was thinking of something along the lines of

switch (bla)
{
case 1, 3, 5:
code ...
break;
case 2, 4, 9:
code ...
break;
case 10..30:
code ...
break;
default:
code ...
break;
}
 
P

Peter Morris

switch (bla)
{
case 1, 3, 5:
code ...
break;
case 2, 4, 9:
code ...
break;
case 10..30:
code ...
break;
default:
code ...
break;
}


Now try that with an enum

MyThingKind.One, MyThingKind.Two, MyThingKind.Three

It gets messy really quickly. The 10..30 part is probably an improvement,
but I don't recall ever actually using it in Pascal so I've never missed it.
 
R

Rudy Velthuis

Peter said:
Now try that with an enum

MyThingKind.One, MyThingKind.Two, MyThingKind.Three

It gets messy really quickly.

Not really:

case MyThingKind.One, MyThingKind.Two, MyThingKind.Three:
code;
break;

or:

case MyThingKind.One..MyThingKind.Three:
code;
break;

Not much messier than:

case MyThingKind.One:
case MyThingKind.Two:
case MyThingKind.Three:
code;
break;

They could have actually done away with break completely and used:

case A:
code;
case B:
code;
case C:
{
code;
code;
}

This would have been analogue to for(), if() and while(). Would have
been a little cleaner, IMO. But probably too far away from C++ and Java.
The 10..30 part is probably an
improvement, but I don't recall ever actually using it in Pascal so
I've never missed it.

I have used it quite often, especially for character ranges like
'0'..'9', 'A'..'Z', but also for other ranges of values.

Oh well, any language has its syntactical highlights and its
syntactical problems. Not really relevant here, I guess. <g>

--
Rudy Velthuis http://rvelthuis.de

"Everywhere I go I'm asked if I think the university stifles
writers. My opinion is that they don't stifle enough of them."
-- Flannery O'Connor (1925-1964)
 
P

Peter Morris

Not much messier than:

Exactly, it is messier :)

Oh well, any language has its syntactical highlights and its
syntactical problems. Not really relevant here, I guess. <g>

I expect discussing the C# language is probably one of the most on-topic
posts seen in this newsgroup for a while :)
 
M

Michael Starberg

They could have done it the Pascal way. No problems with fall-through
or common typo bugs there. <g>

But if I remember my Delphi7 correctly,
you could only switch on integers,
which made the case-statement almost useless.

- Michael Starberg
 
P

Peter

Peter said:
Now try that with an enum

MyThingKind.One, MyThingKind.Two, MyThingKind.Three

switch
{
using (MyThingKind)
{
case One, Two, Three:
doStuff();
}
}
 
P

Peter Morris

switch
{
using (MyThingKind)
{
case One, Two, Three:
doStuff();
}
}

That's changing more than the way the switch statement works, it would also
require that the compiler change to assume the context when in a switch
statement, which wouldn't work because you could easily have a const
somewhere

const string One = "1";

Does "One" refer to the const, or to the MyThingKind.One enum? It would
save some typing, but could prove ambiguous I think.
 
P

puzzlecracker

That's changing more than the way the switch statement works, it would also
require that the compiler change to assume the context when in a switch
statement, which wouldn't work because you could easily have a const
somewhere

const string One = "1";

Does "One" refer to the const, or to the MyThingKind.One enum?  It would
save some typing, but could prove ambiguous I think.

Well, the point of my fall through was that some of the cases had a
part of common functionality which i inserted in the last case, thus i
cannot just use one case.
 
R

Rudy Velthuis

Peter said:
Exactly, it is messier :)

Well said:
I expect discussing the C# language is probably one of the most
on-topic posts seen in this newsgroup for a while :)

Probably, but the language is as it is, and I doubt we will change it
by discussing how we would have liked to see it being more like another
language. So this dicussion is pretty irrelevant. Discussions about how
things actually are, are much more relevant.

--
Rudy Velthuis http://rvelthuis.de

"The chain reaction of evil -- wars producing more wars -- must
be broken, or we shall be plunged into the dark abyss of
annihilation." -- Martin Luther King, Jr.
 
R

Rudy Velthuis

Peter said:
switch
{
using (MyThingKind)
{
case One, Two, Three:
doStuff();
}
}

Nah, ugly. If he wants vertical alignment:

case MyThingKindOne,
MyThingKindTwo,
MyThingKindThree:

I would rather have horizontal alignment. His format is more or less
like specifying every parameter of a function on its own line. <g>

--
Rudy Velthuis http://rvelthuis.de

"One of the symptoms of an approaching nervous breakdown is the
belief that one's work is terribly important."
-- Bertrand Russell (1872-1970)
 
P

Peter

Peter said:
That's changing more than the way the switch statement works, it
would also require that the compiler change to assume the context
when in a switch statement, which wouldn't work because you could
easily have a const somewhere

const string One = "1";

Does "One" refer to the const, or to the MyThingKind.One enum? It
would save some typing, but could prove ambiguous I think.

:)

There's the smiley I forgot to add. It really was tongue-in-cheek. I
have no problems with the syntax for c#'s switch statement.

If I have a lot of stuff to put in a case, or common functionality for
some cases, I'd usually write separate methods to handle that, and call
them from the appropriate cases.

Doesn't Pascal have some sort of "using" syntax? How does it handle
"using (MyThingKind)" when there is a MyThingKind.One and another
entity called "One"? (Guess I could ask in a Pascal group if I was
terribly worried about this issue).

/Peter K
 
P

Peter Morris

Well, the point of my fall through was that some of the cases had a
part of common functionality which i inserted in the last case, thus i
cannot just use one case.
<<

In which case you explicitly state that you want to "fall through"


switch (k)
{
case MyThingKind.One:
DoSomething();
goto case MyThingKind.Common;

case MyThingKind.Two:
SoSomethingElse();
goto case MyThingKind.Common;

case MyThingKind.Common:
DoSomethingCommon();
break;
}


You cannot accidentally type that "goto case" statement but you can
accidentally forget to put in a "break", which I often do especially when
changing a NotImplementedException to implementation code.
 
P

Peter Morris

Doesn't Pascal have some sort of "using" syntax?

You really never want to go there. I seem to remember reading an online
interview with Anders H where he was asked if C# would get a "with"
statement, he said "No". :)

Although it was available in Delphi I stopped using it very early on because
bugs could easily creep into your code as a consequence of using it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top