switch-statement

How can I get rid of this smell? (refactoring switch statement)

Yesterday i was playing with jQGrid plugin and ASP.NET. Everything turned out well, my grid is working now, but i have two methods, that make my code smell. smelly methods: private IOrderedEnumerable<Employee> GetOrderedEmployees(Column sortColumn, bool ascending) { switch (sortColumn) { case Column.Nam...

How can I change the connection speed of a port programmatically?

Hello, I need to change the connection speed of a port on a switch via program or script. Possible choices are 100M Full Duplex, 10M Full and 10M Half Duplex. The server runs a Linux Debian. The switches are "talked to" via SNMP. How can I do it? Best would be Perl and/or PHP. ...

[PHP] Multiple Seperate Switch Statements

Hi :) I have multiple switch statements in on one of the pages in order to pass different vsriables to the URL, as well as different case. I need these different switch statements because I need the different variables. However, when I put a "default" in one of the switch statements, that default applies to every other switch statement...

Does anyone disagree with the statement: "using switch is bad OOP style"?

I have seen it written in multiple threads/comments on stackoverflow that using switch is just bad OOP style. Personally I disagree with this. There will be many cases where you cannot add code (i.e. methods) to enum classes you want to switch on because you don't control them, perhaps they are in a 3rd-party jar file. There will be oth...

Refactor my C# code - Switch statement

I have the following code which I am are currently using .... Basically, this method assigns the correct boolean flag (TRUE/FALSE) for each Task. As more and more tasks need to be added .. I can see that the switch statement will have to grow to cater for every task. There has to be an easier way ... to keep the method small. Code: (fo...

JQuery toggle VAT switching

Hi , I want to implement a VAT switcher as you can see on http://aria.co.uk top right corner although I want to do it on the client side only. Below is something I came up with, although I need to: switch VAT back and forth (my example goes just one way) save and read the toggled state from a cookie for persistency have it unobtrusiv...

Is there a way of performing some code within a Switch statement that executes only if any of the cases have been passed ?

Is there a way that I can execute the same line of code for each "Case" but only have to type it in once instead of having the same code specified for all Cases ? switch (SomeTest) { case "test1": { // Do something for test 1 break; } ...

How to hide back button in the first view on iPhone?

I add 3 views for an application and they need to switch from 1-2-3. Now I add a toolBar and a button 'OK' on the bottom of SwitchViewController, so they can share the button to go to next view. But how to add a 'Back' button so that I can switch from 2-1 or 3-2 and the button shouldn't be seen in the first view? Are there any other w...

Choosing between different switch-case replacements in Python - dictionary or if-elif-else?

I recently read the questions that recommend against using switch-case statements in languages that do support it. As far as Python goes, I've seen a number of switch case replacements, such as: Using a dictionary (Many variants) Using a Tuple Using a function decorator (http://code.activestate.com/recipes/440499/) Using Polymo...

C#: How can I combine a switch with an if-statement?

I need to combine a switch with an if-statement. How can I do that? I want to do something like this: switch (periodtype) { if(starttime>endtime) { ; } else { case 0: nextRunTime = nextRunTime.AddHours(period); break; case 1: nextRunTime = nextRunTime.AddMinutes(period); break; case 2...

Should I assume two-elements enums will remain this way?

It happens quite a lot that I've a two member enum : enum E{ A, B } Let's say I want to assign a different value to i according to my enum value. should I write this : int i= (e== E.A)?0:1; or this : int i; if (e==E.A) i=0; else if (e==E.B) ...

Java switch cases: with or without braces?

Consider the following two snippets, with braces: switch (var) { case FOO: { x = x + 1; break; } case BAR: { y = y + 1; break; } } Without braces: switch (var) { case FOO: x = x + 1; break; case BAR: y = y + 1; break; } I know that, in the snippet with braces, a new scope is created...

SLM248P Linksys/Cisco Switch Web Interface

I have some SLM248P linksys switchs. It says you can connect do it via a web browser. My question was how do i know the switches ip address is? ...

Why switch statement cannot be applied on strings?

int main() { switch(std::string("raj")) //Compilation error - switch expression of type illegal { case"sda": } } ...

Replace giant switch statement with what?

I have a code that parses some template files and when it finds a placeholder, it replaces it with a value. Something like: <html> <head> <title>%title%</title> </head> <body bgcolor="%color%"> ...etc. In code, the parser finds those, calls this function: string getContent(const string& name) { if (name == "title") re...

What is (or should be) the cyclomatic complexity of a virtual function call?

Cyclomatic Complexity provides a rough metric for how hard to understand a given function is, or how much potential for containing bugs it has. In the implementations I've read about, usually all of the basic control flow constructs (if, case, while, for, etc.) increase the complexity of a function by 1. It appears to me given that cyclo...

how to avoid nested switch?

I'm creating a contextual menu in javascript for a web app. The menu can appear in numerous contexts, and have different choices. I could have a different fuction for each context/choice: grid1_delete() grid1_duplicate() grid2_delete() grid2_add() grid2_duplicate() and hard code those in as the menu is being built. The thing I don't l...

What is the difference between IF-ELSE and SWITCH?

Can someone please explain this to me? ...

does this switch statement smell bad?

Switch(some case) { case 1: // compute something ... return something; break; case 2: // compute something ... return something; break; /* some more cases ... */ case X: // compute something ... return something; break; de...

Best way to build object from delimited string (hopefully not looped case)

Hi - this question feels like it would have been asked already, but I've not found anything so here goes... I have constructor which is handed a string which is delimited. From that string I need to populate an object's instance variables. I can easily split the string by the delimited to give me an array of strings. I know I can simply...