switch

Break tag inside of a method

I have a switch statement that executes some logic over and over. Rather then use cut and paste I wanted to put it into a function, but I am failing badly at this. This is what I want to do, but it does not compile because the break tag in the function does not exist. Can anyone refactor this to a better working version? switch(param...

.NET: switch vs dictionary for string keys

I've got a situation where I have a business object with about 15 properties of different types. The business object also has to implement an interface which has the following method: object GetFieldValue(string FieldName); I can see 2 ways of implementing this method: Use a switch statement: switch ( FieldName ) { case "Field1"...

C# iterate over switch's cases

Hello, Is it possible to retrieve programmatically all the case of a switch ? I don't have any idea, maybe by IL but not sure how to do ... In fact my global issue is the following : I got a siwtch case with string as property name. The method is very important and a regression is not allowed. I don't want a refactoring breaking this, ...

Switchable Unique Identifier in C#

I'm implementing a system to send Messages between different parts of a program I'm writing. There are some generic message types as well as some specific to each part of the program. I would like to avoid the hierarchy rot inherent in deriving from a base message class for each type, So i'm encapsulating this type in an int or ushort....

PHP: two values in the case of Switch?

If "car" or "ferrari" as an input, it should print "car or ferrari". How can I achieve it? <?php $car ='333'; switch($car) { case car OR ferrari: print("car or ferrari"); break; case cat: print("cat"); break; default: print("default"...

Jquery Image Switch

Hey guys, I am using an image switch function in Jquery for a site I am building. There are a ton of projects so I am heavily compressing a lot of these images. Some of them however dont look nice as .gifs and it really only makes sense to make a .jpg. My problem is that this code only swaps one image, to another image of a different n...

How to break out of a loop from inside a switch?

Hi, I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE: // ... break; // ... more stuff ... case DONE: break; // **HERE, I want to break out of the loop itself** } } Is there any direct way to do that? I know I can use a flag, and break from the loop by pu...

switch statement in Jquery and List

Hi, I would like to know if my approach is efficient and correct. my code is not working though, I don't know why. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; <html xmlns="http://www.w3.org/1999/xhtml"&gt; <head> <meta http-equiv="Content-Type" content="te...

What is the standard approach for implementing a one-time Boolean switch?

Let's say I have some code like the following, and that processData gets executed hundreds or even thousands of times per minute: class DataProcessor { private: DataValidator* validator; bool atLeastOneDataPoint; bool dataIsValid(Data* dataToValidate) { return validator->validate(dataToValidate); } public: /...

Switch Programming Language

I've forgotten how to switch programming languages in Visual Studio 2008. I need to switch from C++ to C#. Help! ...

Switching branches in git

Sometimes I'm in a feature branch, but I've made an unrelated change that I want to see in master. Often I can just do: git checkout master git commit -m "..." filename But sometimes when I do the checkout I get a warning that there are local changes and thus I can't switch the branch. Why does this only happen sometimes? Is there ...

Evaluate Expressions in Switch Statements in C#

I have to implement the following in a switch statement: switch(num) { case 4: // some code ; break; case 3: // some code ; break; case 0: // some code ; break; case < 0: // some code ; break; } Is it possible to have the switch statement evaluate case < 0? If not, how could I do that? ...

iPhone app switching

Is it possible to run an iPhone app from your own app? For example when you press a button in your app, the iPhone switches to another app. If so, how would you do this? ...

Why do I need to use break?

I was wondering why C# requires me to use break in a switch statement although a fall-through semantics is by definition not allowed. hence, the compiler could generate the break at the end of each case-block and save me the hassle. However, there is one scenario (which has already been discussed on this site) which I could come up wit...

Proper perl switch formatting in emacs

Edit: After reading the responses, I believe the answer is "don't do this", hence I marked an appropriate response as the official answer. Is there an easy way to get emacs to display perl switch statements like perldoc.perl.org's switch page? Here's the formatting on perldoc.perl.org: use Switch; switch ($val) { case 1 ...

need to create a summary of a large switch statement in C#

Alright, i dont know how to explain it well.. but i have a switch statement, string mystring = "hello"; switch(mystring) { case "hello": break; case "goodbye": break; case "example": break; } of course this is an example, and in the real situation, there will be different things happening for each case. ok, hope you get the point, now...

iPhone Switch views _KILL ! Problem

hi guys ... i have a problem with switch view between 2 views with nib files ! here my code . my first page goes to page 2 ! but at page 2 i cant back to first page ! my app go out .. here is my code : from page 1 to 2 : #import "HafezViewController.h" #import "GhazaliateHafez.h" -(IBAction)gh:(id)sender { HafezViewController...

Is this a bad pattern? (Switch inside for/foreach loop)

I find myself writing code such as: foreach($array as $key => $value) { switch($key) { case 'something': doSomething($value); break; case 'somethingelse': doSomethingElse($value); break; } } Is there a better way to go about this? Seems dirty to me, but I might j...

C++ Program wont quit, using switch case

I'm writing a tic tac toe program that plays throuh the terminal/console After Player 1 or 2 wins, I give the choice for the user to play again, 1 = play again, 2 to quit. However, entering 2 to quit doesnt work //tie check, replay, use pointer notation #include <iostream> using namespace std; void initialize(char [][3]); void player1(...

Performance difference in alternative switches in Python.

I have read a few articles around alternatives to the switch statement in Python. Mainly using dicts instead of lots of if's and elif's. However none really answer the question: is there one with better performance or efficiency? I have read a few arguments that if's and elifs would have to check each statement and becomes inefficient wi...