switch-statement

Objective-C switch defines contents of string

Hi, I've come from a java background, so i'm still getting to grips with some of the ways of doing things with Obj-C. Depending on a number provided, I want an NSString variable to have different contents. In java I would do something like this: string foo; switch (numberToSwtich){ case 1: foo = "Something!"; break...

reusing switch statement logic

What is the best way to reuse switch logic. I have this switch statement that keeps appearing in my code. Rather then copy paste it I would like to make a function that calls other delegates and pass these delegates in as parameters. Or is there a better way? Function 1: switch (test) { case "x": ...

How can I use a function pointer instead of a switch statement?

How can I use a function pointer instead of a switch statement? ...

Can this kernel function be more readable? (Ideas needed for an academic research!)

Following my previous question regarding the rationale behind extremely long functions, I would like to present a specific question regarding a piece of code I am studying for my research. It's a function from the Linux Kernel which is quite long (412 lines) and complicated (an MCC index of 133). Basically, it's a long and nested switch...

final public static ints can't be used in a switch statement?!

I'm confused. The following code has errors ("..." represents elided code): int byteOrder = ...; switch (byteOrder) { case HDF5Constants.H5T_ORDER_BE: return ByteOrder.BIG_ENDIAN; ... } The error is on the case statement and Eclipse complains "case expressions must be constant expressions". I looked in the source file ...

Java application if/case recommendation

I am writing an application for a Java course. I am a complete beginner and am just going off material I have learned from the course and from the web. The application is exhibiting some behavior and I am not sure what is causing it. The application is GUI based and does calculations on user input. For the action listener section, I hav...

switch statement and loops using jquery/javascript

Is there a way I can generate switch statements in jquery/javascript by using some sort of loop to do the work for me? For example, if I had a statement like: switch ($("#play option:selected").text()) { case '1': $("#play_1").slideDown().find("input").addClass("someClass"); break; case '2': $("#play_1"...

Building a language switcher - 2 languages only - ASP.NET MVC

ASP.NET MVC app, close to completion, then it was decided that it needed a language switcher. It will simply be two languages: English and Japanese. They should be able to switch between the two, but if one is not selected it should choose whichever is the default language of the browser. I'm wondering what the best solution would be in...

Using Java Generics with Enums

Update: Thanks for everyone who helped out - the answer to this one lay in what I wasn't noticing in my more complex code and what I didn't know about the Java5 covariant return types. Original Post: I've been playing around with something this morning. While I know that I could tackle this whole problem differently, I'm finding mysel...

Weird Switch error in Obj-C

I have this switch statement in my code: switch(buttonIndex){ case 0: [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES]; break; case 1: UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerContro...

Why doesn't my regular expression work with Perl's Switch module?

Hi, I wanted to use a switch statement. I ran into difficulties quickly. It looks like I was unlucky. I decided to use if else style instread of the switch. I wonder why this does not work. What about you? It looks like there is problem with /gc flags on the regular expression. use Switch; while ( pos($file) < length($file) ) { swit...

Counting vowels using switch

I'm new to C#. While learning it, I tried to design a program which count the vowels in a sentence. In my code, I used the "foreach" statement with the if-else if statement. I would like to convert these line of code using the switch statement but I'm not really sure where to go. Do I need to add a new method? I would appreciate your h...

Architecture Design for DataInterface - remove switch on type

I am developing a project that calculates various factors for a configuration of components. The configuration is set/changed by the user at runtime. I have a Component base class and all configuration items are derived from it. The information for each component is retrieved from data storage as and when it is required. So that the sto...

referencing each case of a switch from a conditional inside a different method in java

Hello, I am implementing some methods which use switch statements to distinguish between different cases: private void doThis(){ switch(command){ case on: {status = doCalculationsA; break;} case off: {status = doCalculationsB; break;} case idle: {status = doCalculationsC; break;} case stdby:{status = doCalculationsD;...

How should i "build" things now I've implemented polymorphism? (Java, sim game)

Hi all. I'm working on a small java game which is a sort of clone of the 1995 game Theme Hospital. I've recently been working on the GUI and the Level structure now I have the path finding and grid working. My current way of "building stuff" consists of one room and one reception desk, and it works ok so far, but I need to implement a wa...

Using properties in switch statement cases?

I have a switch statement in one class that generates Command objects, based on if a certain line in a code file matches the particular code for that Command object. like this: switch (line): { case "#CODE1": return new Command1(); case "#CODE2": return new Command2(); } I want to turn it into something like this:...

Declaring variables inside a switch statement

I saw a few answers to this issue, and I get it — you can't declare and assign variables inside a switch. But I'm wondering if the following is correct at throwing an "error: expected expression before 'int' switch (i) { case 0: int j = 1; break; } Why would putting an NSLog before it result in no errors? switch ...

PHP: default as first option in switch statement?

I've tested this and it works fine, but it looks... weird... to me. Should I be concerned that this is nonstandard form which will be dropped in a future version of PHP, or that it may stop working? I've always had a default case as the final case, never as the first case... switch($kind) { default: // The kind wasn't valid, se...

How to store linear range of values? Which Data structure to choose?

I need a simple structure to store polygon names based on sides ... so for e.g. 1 side is "monogon", 2 sides is "digon", 3 sides is "triangle" and so on (say for upto 12 sides) What is the simplest way to store these and reuse them in the code dynamically? for e.g. if my polygonShape class has 3 as the number of sides, it should return "...

Are there any conventions for flowcharting that distinguish a switch from a if-else chain?

I had to do a overview for a customer meeting, and they requested flow charts. It had never occurred to me that there was no switch symbol in any of the flow charting I've seen. I know functionally they are similar, but documentation should represent the code you've written or are planning too. Maybe I'm just being picky, but it seem...