Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good substitute for a big switch-case?

I have objects called Country. At some point in the program, I want to set the field power of each object.

The power for each country is fixed and I have data for all 196 countries here on a piece of paper. My code should check, for instance, if the country's name is USA (and if so, set its power to 100) and so on.

I know I can do it with a switch-case, but what is the best, nicest, and most efficient way to do it?

like image 733
nasim Avatar asked Dec 17 '14 15:12

nasim


People also ask

What can I use instead of a switch case?

Luckily, JavaScript's object literals are a pretty good alternative for most switch statement use-cases I can think of. The idea is to define an object with a key for each case you would have in a switch statement. Then you can access its value directly using the expression you would pass to the switch statement.

Which of the following is the best alternative for switch case?

char, byte, short can be used in switches too.

What can be use instead of switch case in C++?

In this case, you could also use std::array<int, 5> , which, unlike std::vector<int> , can be constexpr . This worked perfectly, thank you so much! Switch statement can be removed!

Is switch case necessary?

No it is not necessary of default case in a switch statement and there is no rule of keeping default case at the end of all cases it can be placed at the starting andd middle of all other cases.


2 Answers

You can store country-power pairs into a Dictionary<string, int> then just get the score of a particular country by using indexer:

var points = new Dictionary<string,int>(); // populate the dictionary... var usa = points["USA"]; 

Edit: As suggested in comments you should store the information in external file, for example an xml would be a good choice. That way you don't have to modify the code to add or remove countries. You just need to store them into XML file, edit it whenever you need.Then parse it when your program starts, and load the values into Dictionary.You can use LINQ to XML for that.If you haven't use it before there are good examples in the documentation to get you started.

like image 115
Selman Genç Avatar answered Oct 05 '22 22:10

Selman Genç


Whilst Selmans answer is right and good, it does not answer how to actually populate the Dictionary. Here is it:

var map = new Dictionary<string, int> {     {"USA", 100},     {"Germany", 110} }; 

you may however also just add it as follows:

map.Add("USA", 100); map.Add("Germany", 110); 

Now you may access the value (as already mentioned by Semans):

map["USA"] = 50;        // set new value for USA int power = map["USA"]; // get new value 

EDIT: As already mentioned within comments and other answers you may of course store the data within an external file or any other data-storage. Having said this you may just initialize an empty dictionary and then fill it with the Add-method previously mentioned for every record within that storage.

like image 25
MakePeaceGreatAgain Avatar answered Oct 05 '22 21:10

MakePeaceGreatAgain