switch-statement

break in a case with return.. and for default

My OCD makes me add "break" when writing case statements, even if they will not be executed. Consider the following code example: switch(option) { case 1: a = 1; b = 7; break; case 2: a = 2; b = 4; return (-1); break; default: a = -1; break; } My two ...

In Java switch statements on enums, why am I getting a compilation error when I qualify my values in each case?

I have a switch statement in Java, on an Enum which let us call IMyInterface.MyEnum Each of my case statements has the form: IMyInterface.MyEnum.MyValue, (though I could drop the IMyInterface if I imported). However, the compiler (Java 6) throws an error: "The qualified case label IMyInterface.MyEnum.MyValue must be replaced with the u...

switch statement

Hi I'm practicing the "Switch Loop" in a program. And I'm making a code where a user can input the integer and after the user will input the integer it will also display what the user just typed in. Now I'm trying to implement where the program will ask the user to input the number again by selecting the Y/N. I already included it here i...

Network switching and VLAN

if I had 2 managed switches with vlans configured like: Switch 1: Server - Vlan 1 2 3 Desktop - Vlan 1 Switch 2: Desktop - Vlan 2 Desktop - Vlan 2 Access Point - Vlan 3 Switches are connected through 2 gigabit uplink ports would every desktop / wireless client see the server? (im not sure if you ...

consecutive if's in PHP

Hi, I'm doing some validation now in PHP, and I'm running allot of conditional statements, for example: if ($this->email->isValid($email)) return false; if ($this->username->isValid($username)) return false; ect.. Is there a nice way to do this? Or do I just run ten If statements like the ones above? I can't use switch obviously,...

Using the switch statement in a Windows Forms application

Hi I'm new to C#. And I'm doing some exercises about switch. I just did it from console application and I would like to do it in window forms applications. I'm looking for syntax on how to do switch in window forms. In console it's usually like this: switch (wordValue) { case 1: Console.WriteLine("You have entered numbere...

What is the meaning of the GCC warning "case label value exceeds maximum value for type"?

My code looks like this: char * decode_input(char ch) { switch(ch) { case 'g': return "get"; break; case KEY_F(9): return "quit"; break; default: return "unknown"...

Switch statement and isset

Since a couple of months ago my website was full of notice errors so I'm trying to fix the code now. I suppose there was some php update. I'm not so bright at php so: switch ( (isset($_GET['action'])) ) { case "delete": delete(); break; } this won't work when I add isset. Can't you use isset inside a function? I dont know really so...

Why Switch/Case and not If/Else If?

This question in mainly pointed at C/C++, but I guess other languages are relevant as well. I can't understand why is switch/case still being used instead of if/else if. It seems to me much like using goto's, and results in the same sort of messy code, while the same results could be acheived with if/else if's in a much more organized m...

C++/CLI switch on String

In other .NET languages such as C# you can switch on a string value: string val = GetVal(); switch(val) { case "val1": DoSomething(); break; case "val2": default: DoSomethingElse(); break; } This doesn't seem to be the case in C++/CLI System::String ^val = GetVal(); switch(val) // Compile error { // Snip } Is there a sp...

Switch on Enum (with Flags attribute) without declaring every possible combination ?

Hello, how do i switch on an enum which have the flags attribute set (or more precisly is used for bit operations) ? I want to be able to hit all cases in a switch that matches the values declared. The problem is that if i have the following enum [Flags()]public enum CheckType { Form = 1, QueryString = 2, TempData = 4, } and i ...

Would you use regions within long switch/enum declarations?

I've recently found myself needing (yes, needing) to define absurdly long switch statements and enum declarations in C# code, but I'm wondering what people feel is the best way to split them into logical subsections. In my situation, both the enum values and the cases (which are based on the enum values) have fairly clear groupings, yet ...

Switch Statement for Enum Value Representations in Java

I'm very familiar with using Enums in other languages, but I'm having some difficulty in Java with a particular use. The Sun documentation for Enums boldly states: "Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers." Well, that's dandy, ...

Would it be ridiculous to use a switch statement to handle irc server codes?

There are quite a few of IRC server codes I am working on a small IRC client for Adobe AIR, and I started out by supporting only a few of these initially, and then a switch statement didn't seem like a bad idea. But as I support more and more, the switch statement is getting longer and it feels like it's a little out of control. One iss...

Java: If vs. Switch

I have a piece of code with a) which I replaced with b) purely for legibility ... a) if ( WORD[ INDEX ] == 'A' ) branch = BRANCH.A; /* B through to Y */ if ( WORD[ INDEX ] == 'Z' ) branch = BRANCH.Z; b) switch ( WORD[ INDEX ] ) { case 'A' : branch = BRANCH.A; break; /* B through to Y */ case 'Z' : branch = BRANCH.Z; brea...

When should one try to eliminate a switch statement?

I've come across a switch statement in the codebase I'm working on and I'm trying to figure out how to replace it with something better since switch statements are considered a code smell. However, having read through several posts on stackoverflow about replacing switch statements I can't seem to think of an effective way to replace thi...

Any tips for switching from CakePHP to Ruby on Rails?

I've been wanting to make the switch from PHP. Anyone care to highlight the similar classes or the key differences between CakePHP and Ruby on Rails? Thank you! ...

Switch statement without default when dealing with enumerations

This has been a pet peeve of mine since I started using .NET but I was curious in case I was missing something. My code snippet won't compile (please forgive the forced nature of the sample) because (according to the compiler) a return statement is missing: public enum Decision { Yes, No} public class Test { public stri...

Can I declare variables inside an Objective-C switch statement?

I think I'm going blind, because I can't figure out where the syntax error is in this code: if( cell == nil ) { titledCell = [ [ [ TitledCell alloc ] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier ] autorelease ]; switch( cellNumber ) { case 1: NSString *viewDataKey = @"Name"; etc... When I try to...

Eliminating Ugly Switch Statement When Retrieving Resources

I am adding a splash screen to a .NET compact application and am wondering if there's an elegant way to access the correct bitmap (based on screen resolution) for the splash screen. e.g. My resource bitmap properties are named like this... Splash640480 Splash480640 Splash480480 Splash320240 Splash240320 Splash240240 ... etc I tr...