简体   繁体   English

什么是大型开关盒的良好替代品?

[英]What is a good substitute for a big switch-case?

I have objects called Country. 我有一个名为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. 每个国家的权力是固定的,我在这里有所有196个国家的数据。 My code should check, for instance, if the country's name is USA (and if so, set its power to 100) and so on. 例如,我的代码应检查国家/地区的名称是否为USA(如果是,则将其功率设置为100),依此类推。

I know I can do it with a switch-case, but what is the best, nicest, and most efficient way to do it? 我知道我可以用开关盒做到这一点,但是最好,最好,最有效的方法是什么?

You can store country-power pairs into a Dictionary<string, int> then just get the score of a particular country by using indexer: 您可以将国家/电源对存储到Dictionary<string, int>然后使用索引器获取特定国家/地区的分数:

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. 编辑:正如评论中建议的那样,您应该将信息存储在外部文件中,例如xml将是一个不错的选择。 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. 您只需将它们存储到XML文件中,然后在需要时进行编辑。然后在程序启动时解析它,并将值加载到Dictionary 。您可以使用LINQ to XML 。如果您之前没有使用它文档中的好例子可以帮助您入门。

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): 现在您可以访问该值(如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. 说完这个之后,您可以初始化一个空字典,然后使用前面提到的Add-method填充该存储中的每个记录。

This is the right question to begin with, but there are a lot of things you need to learn. 这是一个正确的问题,但是你需要学习很多东西。 Many folk have given you answers to the question you asked. 很多人都给你答案。 I'm going to be annoyingly Zen and tell you to unask the question because there is a larger problem to resolve. 我会讨厌禅宗并告诉你解开这个问题,因为要解决一个更大的问题。

Instead of hard coding this, store the related properties in an n-tuple also known as a database row and use a database engine to manage the relation between the two. 而不是硬编码,将相关属性存储在n元组(也称为数据库行)中,并使用数据库引擎来管理两者之间的关系 And then since you are using C# it would probably be smart to learn to use LINQ. 然后,因为您正在使用C#,所以学习使用LINQ可能很聪明。 But before you do that, learn a bit of data modelling theory, because data-modelling is what you are doing. 但在此之前,请先学习一些数据建模理论,因为数据建模就是您正在做的事情。

Since you said you have "objects" called "Country", and you have tagged your question "C#", it would seem that you have two forces at work in your code. 既然你说你有“对象”称为“国家”,并且你已经标记了你的问题“C#”,那么你的代码中似乎有两种力量在起作用。 One is that having to refer to a map, however efficiently implemented, is not as cheap as referring to a member variable . 一个是必须引用地图,无论如何有效实施,都不如引用成员变量那么便宜。 On the other hand there might be some benefit to a setup where all the attributes of a country can be found in the same place as the attributes of other countries (the map-oriented solutions do address this concern). 另一方面,对于一个设置可能会有一些好处,其中一个国家的所有属性可以在与其他国家的属性相同的地方找到(面向地图的解决方案确实解决了这个问题)。 But these forces can be reconciled something like this: 但这些力量可以和解如下:

class Country { // Apologies that this sketch is more C++ than C#
public:
   Country(string name_, int power_);
private:
   string name;
   int power;
};
void MakeCountries()
{
    countries.Add(new Country("USA", 50));
    countries.Add(new Country("Germany", 60));
    // ....
}

Do you need to update your data at runtime? 您是否需要在运行时更新数据?

What about making an array of Strings for storing country names in ascending order of their power. 如何制作一个字符串数组,以便按照其权力的升序存储国家/地区名称。 It will be more simple to implement.Then the index of each country can represent its power. 实施起来会更简单。然后每个国家的指数可以代表它的力量。 This is possible, only if the power is continues counting numbers. 这是可能的,只有当电源继续计数时才有可能。

If its not , another siple way is to implement them as linked list. 如果不是,另一种方法是将它们作为链表实现。 So that u will be able to change if u want. 如果你愿意,你将能够改变。 A list with 2 fields; 包含2个字段的列表; 1for the country and other for the power 1为国家和其他为权力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM