tags:

views:

136

answers:

4

So I want to have a property getter that returns an enum value based on an enum value of a different type without having to resort to a long switch statement. Is there a way to match up the two enum lists using an index or values?

public enum LanguageName
    {
        Arabic,
        Chinese,
        Dutch,
        English,
        Farsi,
        French,
        Hindi,
        Indonesian,
        Portuguese,
        Spanish,
        Urdu
    }

    public enum LanguageISOCode
    {
        ara,
        zho,
        dut,
        eng,
        fas,
        fre,
        hin,
        ind,
        por,
        spa,
        urd
    }

public class language
{
    public language()
    {
    }

    public LanguageName Name
    {
        get
        {
           // get the Name enum based on the Code enum
        }
        set;
    }

    public LanguageISOCode Code
    {
        get;
        set;
    }
}
+2  A: 

Assuming the values are in the same order, you can just go through an int conversion first:

public LanguageName Name
{
    get
    {
        int n = (int) Code;
        return (LanguageName)n;
    }
}

This would require that make sure your enumerated values are always in the right order, or that you explicitly assign them numerical values:

public enum LanguageName
{
    Arabic = 1,
    Chinese = 2,
    ...
}

public enum LanguageISOCode
{
    ara = 1,
    zho = 2,
    ...
}

The above is not required (since the standard guarantees that they get assign monotonically increasing numerical values) but at least it covers you in the case where you might decide to reorder fields, or something like that.

Dean Harding
+1  A: 

You can use same int value in both, and cast as int and cast as enum.

public enum LanguageName
    {
        Arabic = 1,
        Chinese,
        Dutch,
        English,
        Farsi,
        French,
        Hindi,
        Indonesian,
        Portuguese,
        Spanish,
        Urdu
    }

    public enum LanguageISOCode
    {
        ara = 1,
        zho,
        dut,
        eng,
        fas,
        fre,
        hin,
        ind,
        por,
        spa,
        urd
    }

public class language
{
    public language()
    {
    }

    public LanguageName Name
    {
        get
        {
           return (LanguageName)((int)Code);
        }
        set;
    }

    public LanguageISOCode Code
    {
        get;
        set;
    }
}

Or you can use another approach, like use [Description] and make some search.

Zote
+3  A: 

You can use Dictionary<LanguageName, LanguageISOCode> for explicit mapping of one to the other.

Dictionary<LanguageName, LanguageISOCode> languageCodes = new Dictionary<LanguageName, LanguageISOCode> {
  {LanguageName.Arabic,LanguageISOCode.ara},
  {LanguageName.Chinese,LanguageISOCode.zho},
  //...
};

LanguageName lang = LanguageName.Arabic;
LanguageISOCode code = languageCodes[lang];
Igor Zevaka
+7  A: 

You can explicitly declare that one enum value is equal to another, like so:

public enum LanguageName
{
    Arabic,
    Chinese,
    Dutch,
    ...
}

public enum LanguageISOCode
{
    ara = LanguageName.Arabic,
    zho = LanguageName.Chinese,
    dut = LanguageName.Dutch,
    ...
 }

Which will then let you cast between them:

 LanguageISOCode codeValue = LanguageISOCode.ara;

 LanguageName name = (LanguageName)(int)codeValue;

or

 LanguageName nameValue = LanguageName.Arabic;

 LanguageISOCode = (LanguageISOCode)(int)nameValue;

While the above should answer your explicit question, you should have a look at the System.Globalization.CultureInfo class. It provides quite a bit of functionality regarding various cultures.

Adam Robinson
Thanks! You gave me exactly what I suspected was possible. Thanks for mentioning the CultureInfo class.
Dan Bailiff