|
hi
I have these code . What could cause above error ? Thanks
state=1
switch (state)
{
case 1:
//do something
goto case 2;
case 2:
//do something
goto case 3;
case 3:
//do something
goto case 4;
............
case 100:
//do something
}
|
|
|
|
|
dec82 wrote: What could cause above error
goto
|
|
|
|
|
I'm almost speechless! I'll leave it for others to say as I'm in a good mood today.
Look up break (and maybe default: too).
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
YEUCK! I have copied your code to "Coding Horrors"!
This is C#, not C++. Switch blocks cannot fall through in C#, they must end in a break statement.
Please, go and look at a C# book under two sections: "switch statement" and "Don't use goto unless you really, really have to"
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
OriginalGriff wrote: I have copied your code to "Coding Horrors"!
You beat me to it!
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
OriginalGriff wrote: Don't use goto unless you really, really have to
Fixed that for you
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
There are times when a goto can really improve readability, rather than if...if..if..if... etc.
Having said that, I don't think I've used one in a non-assembler language for twenty or so years.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
If you're a student/junior dev though; the rule is NEVER. You're not experienced enough to ID the few cases where it really is the best choice and will just learn bad habits.
It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains.
-- Pride and Prejudice and Zombies
|
|
|
|
|
dan neely wrote: the rule is NEVER
Absolutly true...
Learn the law!
Obey the law!
Fear the law!
Believe the law!
And THEN you can break the law!
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
williamnw wrote: Learn the law!
Obey the law!
Fear the law!
Believe the law!
Judge Dredd wrote: I am the law!!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Then how do you execute multiple cases in a switch in C#?
|
|
|
|
|
LalalalaICantHearYouLalalalalala
Outside of a single school exercise (Generate the complete lyrics for the 12 days of Christmas) I can't think of a single case where falling through was desired behavior.
Seriously though, that's the exception which proves the rule.
It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains.
-- Pride and Prejudice and Zombies
|
|
|
|
|
dan neely wrote: Outside of a single school exercise (Generate the complete lyrics for the 12 days of Christmas) I can't think of a single case where falling through was desired behavior.
It does happen occasionally. My current project has two switches where it was much clearer to add a "goto" rather than convoluted logic.
The important thing, of course, is to comment it properly so everyone can see exactly what's being done, and why.
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
I agree that fall-through is evil, but there are still situations where you want to execute multiple cases; if not with goto, then some other keyword, at least the goto acts as a cry for help.
|
|
|
|
|
OriginalGriff wrote: goto can really improve
Nope - never happenned in, yeah probably 15 years. It never even enters the thought processes as a tool to solve a problem, and I started programming VBA macros way back.
If one of my juniors devs used a goto I'd attack him with a bar of soap, or super glue the g key to his forehead.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I need to convert these C++ code into C# code . What's is the easiest way ? thanks
state=1
switch (state)
{
case 1:
//do something
state++;
case 2:
//do something
if ( )
{
state++;
}
else
{
state=state+5;
}
break;
case 3:
//do something
state++;
............
case 100:
break;
}
|
|
|
|
|
Good question. This code is very C++ - specific. Unfortunately I haven't time to answer you now, but I'm looking forward for someone to provide answer.
Greetings - Jacek Gajek
|
|
|
|
|
Firstly, find the programmer who originally wrote this. Take him outside, and beat him senseless. It's pretty bad code as C++, and can't be directly translated into C# as the later enforces rules to prevent accidental mistakes (such as fall through of cases).
Secondly, get out a pen and paper and do a flow diagram of some sort to work out how this spagetti works. Tidy the diagram up so it can be reliably implemented in any language. Re-code into C#.
Not a quick job by any means, but it's the companies' (or whoevers') fault for allowing such rubbish to be produced in the first place!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
is it possible to replace 'switch' by 'while/for' , and 'case' by 'if'?
thanks
|
|
|
|
|
Why use a case at all, it seems pretty procedural to me. You want to execute a block of code in order, your condition isn't really needed since you always increment 'state', so your select case is useless.
|
|
|
|
|
I'm back.
It depends on what state is equal to. As I have understood, the state=1 line is just an example of possible value which can take state . If state would be fixed to 1 every time, 80% of could be removed, including the whole switch statement.
So the question to dec82: Is state=1 line just an example or is it actually present in the source code?
Greetings - Jacek Gajek
|
|
|
|
|
Not necessarily. As Luc said earlier, this looks like a state machine implemented as a single switch block. As such, it may never exit! It could be accepting inputs from (say) Console.Readline and modifying state as a result. I've seen it done (by crap proceduraly oriented programmers) before.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Kinda cool although unmaintainable a bit. I've posted him a link to my FSA stuff.
Greetings - Jacek Gajek
|
|
|
|
|
Jacek Gajek wrote: Kinda cool although unmaintainable a bit.
Trust me, I'm not defending this code at all - I posted it to "Coding Horrors" - I like the understatement though!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Oh, dear
- Never use goto. It kills babies
- You just drop though each case. Why?
- You do a switch, when you've just set the variable you switch on
- You have one hundred hard-coded cases. Find out how to make that dynamic
- The switch is unnecessary. If you're just dropping through, then it won't make any difference
- You have basically implemented line labels. Why do you have line labels, when you can refactor your code
- No default: option
- The first line of is missing a semicolon
|
|
|
|