分配多个值枚举元素 [英] Assign multiple values to enum elements
问题描述
您好我有这个枚举当前
[Serializable]
public enum Country
{
US = 1,
Canada = 2,
}
当我通常从数据库获取整数I用它转换为枚举
When I usually get the integer from the database I convert it to the enum using
(Country) Convert.ToInt32("1")
我现在有2子地区美国和加拿大,1安培; 2为美国和3和4为加拿大。所以,当我
I now have 2 sub regions in US and Canada, 1 & 2 for US and 3&4 for canada. So when I do
(国家)Convert.ToInt32(1)
或 (国家)Convert.ToInt32(2)
我应该得到的枚举是美国。和3及4加拿大。如何实现这一点?
(Country) Convert.ToInt32("1")
or (Country) Convert.ToInt32("2")
i should get the enum to be US. and for 3 & 4 Canada. How do i implement this?
[Serializable]
public enum Country
{
US = 1,2
Canada = 3,4
}
这样的事情。这可能不是正确的,但只给你一个想法。
Something like this. This is probably not right but just to give you an Idea.
推荐答案
啊我只是用一个函数,而不是直接类型转换。远远比更容易实现完全不同的东西。我已经在这个如此不能改变运行大量的代码是多少,但这里是我做到了。
Ah I just used a function instead of directly typecasting. Far more easier than to implement something entirely different. I already have lot of code running on this so cant change it much but here is what I did.
public Country GetCountryByTaxID(int taxID)
{
if (taxID == 3 || taxID == 4)
{
return Country.USA;
}
else
{
return Country.Canada;
}
}
这篇关于分配多个值枚举元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!