2010-12-21 168 views
14

您好我有這個枚舉當前分配多個值枚舉元素

[Serializable] 
public enum Country 
{ 
    US  = 1, 
    Canada = 2, 
} 

當我通常從數據庫中獲取整數I將其轉換爲使用枚舉

(Country) Convert.ToInt32("1") 

我現在有2子美國和加拿大地區1 & 2美國和加拿大3 & 4。所以當我做

(Country) Convert.ToInt32("1")(Country) Convert.ToInt32("2")我應該得到的枚舉是美國。併爲加拿大提供3 & 4加拿大。我如何實現這個?

[Serializable] 
public enum Country 
{ 
    US  = 1,2 
    Canada = 3,4 
} 

就是這樣。這可能是不對的,但只是給你一個想法。

回答

3

啊,我只是用一個函數,而不是直接類型轉換。比實施完全不同的東西要容易得多。我已經有很多代碼在運行,所以無法改變它,但這是我所做的。

public Country GetCountryByTaxID(int taxID) 
     { 
      if (taxID == 3 || taxID == 4) 
      { 
       return Country.USA; 
      } 
      else 
      { 
       return Country.Canada; 
      } 
     } 
9

你將不得不做這樣的事情:

class Region 
{ 
    static readonly RegionMap = new Dictionary<int,string> 
    { 
     { 1, "US" }, 
     { 2, "US" }, 
     { 3, "Canada" } 
     { 4, "Canada" } 
    } 

    public static string GetRegion(int code) 
    { 
     string name; 
     if (!RegionMap.TryGetValue(code, out name) 
     { 
      // Error handling here 
     } 
     return name; 
    } 
} 

然後查找基於從數據庫中值的字符串:

string region = Region.GetRegion(dbVal); 
+0

我發現這個尋找別的東西。這是對我最理智的答案。當然,這個問題在五點半之前就被問到了! – MarkMYoung 2016-04-19 16:30:48

8

這是不可能的。那麼你必須使用單獨的值。如果名稱相同,即。

[Serializable] 
[Flags] 
public enum Country 
{ 
    US  = 1, 
    Canada = 2, 
    Northern = 4, 
    Southern = 8 
} 

您可以這樣做:Countries = Country.US | Country.Northern。如果沒有,你需要找到另一種方式,可能是另一種財產,或者更好,一個Location類。

5

也許是這樣的?

static Country ConvertRegionToCountry(string region) 
{ 
    switch (region) 
    { 
     case "1": 
     case "2": 
      return Country.US; 

     case "3": 
     case "4": 
      return Country.Canada; 
    } 
} 
13

enum可能不是這樣的問題模型權結構。

我會建議創建一個類來表示國家信息,並提供方法轉換爲數字表示和從數字表示。有了這樣的問題,您還必須決定將選定的國家/地區實例轉換爲數字值時使用的編碼值。

枚舉對象模式可以幫助起點模擬這種情況:

public sealed class Country 
{ 
    // initialize appropriately in the constructor... 
    private readonly int[] m_Values; 
    private readonly string m_Name; 

    // make the constructor private so that only this class can set up instances 
    private Country(string name, int[] codes) { ... } 

    public static Country US = new Country("United States", new[]{ 1,2 }); 
    public static Country Canada = new Country("Canada", new[] {3,4}); 

    public static Country FromCode(int code) { ... } 
    public override string ToString() { return m_Name; } 
    // ... etc... 
} 

根據你的榜樣,你也應該考慮是否需要國家次區域作爲第一級車型實體,而不是簡單地將它們摺疊到您的國家/地區枚舉的實施細節中。您是否應該這樣做取決於您的要求和用例,因此只有您可以對此做出適當的決定。

+0

感謝您的意見,我在同一個項目的其他情況下實施它。改變現有的是非常痛苦的,所以我堅持快速修復。但是這樣好多了。 – tHeSiD 2010-12-21 23:08:25

0

這接縫就像一組問題,對我來說,其中1 & 2組中的美國和3 & 4是在集加拿大。在code project上有一些設置代碼,我認爲這更好地模擬了這個問題。

public static class Convert 
{ 
    public static object ChangeType 
     (object value, Type conversionType, IFormatProvider provider) 
    { 
     int country; 
     if (conversionType == typeof(Country) 
      && int.TryParse(value.ToString(), out country)) 
     { 
      switch (country) 
      { 
       case 1: 
       case 2: 
        return Country.US; 
       case 3: 
       case 4: 
        return Country.Canada; 
      } 
     } 

     // For most cases, including any country unmatched above... 
     return System.Convert.ChangeType(value, conversionType, provider); 
    } 
} 

mscorlib.dll程序Convert類的原始方法:

3
public Country GetCountry(int a) 
     { 
      if (a == 1 || a == 2) 
      { 
       return Country.USA; 
      } 
      else if (a == 4|| a == 3) 
      { 
       return Country.Canada; 
      } 
     } 
0

繼DTB的答案(和那些相當於if建議),我只是在我的應用程序的命名空間中實現的System.Convert的「重寫」的版本現在必須以「System.」作爲前綴,所以任何改進都將受到歡迎!