使用对象vs字符串与枚举 [英] Use of object vs string vs enum
问题描述
它基本上是一个字符串属性,但字符串可以是我不想要的任何东西。一个对象看起来像是过分的,一个枚举意味着如果要添加新的国家或现有的国家,我必须重新编译。
我可以轻松地在GUI中控制,但是我需要在应用程序代码中进行控制。所以我有一个属性可以是一个字符串,一个对象或枚举(或其他)的对象,我不能决定使用哪个。所以我的选择是这样的:
a)在GUI中控制,不要在应用程序代码中检查,冒着我可以得到非法的风险国家名称。
b)创建一个对象(国家/地区)并使用它,这是一个过度的技术,使代码更加复杂,但是我可以完全控制重复和所有
c)使用枚举,希望我不必经常重新编译。它是简单而有效的,但是一个静态的解决方案。
d)使用有效的国家名称的内部字符串列表,并将CountryName作为字符串属性,并确保它已被验证反对那个字符串。我得到验证,CountryName只是一个简单的字符串,但如果我更改有效的国名的内部字符串怎么办?我不得不使代码重新验证程序中的所有制造商对象,也确保它们仍然具有有效的contry名称。
我不完全确定它有多重要有效的国名,但我认为越多,我意识到我处于灰色地带。一个对象或结构太多了,枚举太静,字符串太简单了。
我可能完全超出这里的含义,但我真的很想知道该怎么做,或者更确切地说,当你进入对象的字符串对于枚举。
感谢您的!
Hal
我将使用一个字典存储为一个单独的文件,如 XML属性列表,如下所示:
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE plist SYSTEMfile://localhost/System/Library/DTDs/PropertyList.dtd>
< plist version =1.0>
< dict>
< key> UK< / key>
< string>英国< / string>
< key> FR< / key>
< string>法国< / string>
< key> SP< / key>
< string>西班牙< / string>
< / dict>
< / plist>
I have a situation that may seem ridiculus but I have not been able to figure out a good enough solution. To simplify things the problem is something like this, suppose you have an object like Manufacturer that has a countryname property (like car.Manufacturer.CountryName) and you want to be sure that the countryname property can not have duplicates or misspellings or other errors.
It is basically a string property but a string can be anything which I do not want. An object seems like overkill and an enum means I have to recompile if new countries are to be added or existing countries to be changed.
I could easily control this in a GUI but I need to control this in the application code. So I have an object with a property that could be a string, an object or an enum (or other) and I cant decide which to use. So my options are something like this:
a) Control this in the GUI and do not check this in the application code, taking the risk that I can get "illegal" country names.
b) Make an object (Country) and use that, which is overkill and makes the code more complex, but I have complete control over duplicates and all that stuff.
c) Use an enum and hope that I do not have to recompile too often. It is simple and effective but a static solution.
d) Use an internal string list of valid country names and have CountryName as a string property and make sure that it is validated against that string. I get validation and CountryName is just a simple string, but what if I change that internal string of valid country names? Than I have to make code the revalidates all Manufacturer objects in the program too make sure they still have valid contry names.
I am not totally sure how important it is too have valid country names but the more I think about this the more I realize that I am in a grey zone. An object, or struct, is too much, an enum too static, a string too simple.
I could be totally overcomplicating things here but I would really like too know what to do, or rather how to think, when you get into this grey zone of object vs string vs enum.
Thankfully yours! Hal
I would use a dictionary stored as a separate file, like a XML property list, something like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="1.0">
<dict>
<key>UK</key>
<string>United Kingdom</string>
<key>FR</key>
<string>France</string>
<key>SP</key>
<string>Spain</string>
</dict>
</plist>
这篇关于使用对象vs字符串与枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!