maria Lynx
maria Lynx

Reputation: 59

c# return country abbreviation to country name

I want convert country abbreviation to the full name my code:

public string Convert(string country)
{
    if (country == nameof(AC)) return AC;
    if (country == nameof(AF)) return AF;
    if (country == nameof(AX)) return AX;
    if (country == nameof(AL)) return AL;
    if (country == nameof(DZ)) return DZ;
    return country;
}
public const string
    AC = "ASCENSION ISLAND",
    AF = " AFGHANISTAN",
    AX = " ALAND",
    AL = " ALBANIA",
    DZ = " ALGERIA",
    AD = " ANDORRA",

it works but i want know if it is possible to make it easier. because its too long if i make it for all country.

Upvotes: 0

Views: 677

Answers (3)

Russell Jonakin
Russell Jonakin

Reputation: 1996

I would suggest using a Dictionary to store the Country Code as the Key and the Country Name as the value. Like this:

Public Dictionary<string, string> countryCodes = new Dictionary<string, string>
{
    { "AC", "ASCENSION ISLAND" },
    { "AF", "AFGHANISTAN" },
    { "AX", "ALAND" },
    { "AL", "ALBANIA" }
    //Keep adding countries as you need
};

Then you would not need a convert method to get the country name, you could just use the country code to get value for that country code from the Dictionary.

Like this: (assuming a string variable countryCode with your 2 character country code)

string countryName = countryCodes[countryCode];

Upvotes: 1

I often use enum with attribute for those kind of problem. It is quite convenient and good coding convention. You may try this solution

    using System;
    using System.Reflection;

    namespace CountryEnum
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Using enum
                COUNTRY_CODE enum_variable = COUNTRY_CODE.AF;

                Console.WriteLine("Enum variable: " + Program.GetEnumDescription(enum_variable));

                // Have short code string of country as input -> convert it to enum
                string country_code = "AL";
                COUNTRY_CODE convertResult = COUNTRY_CODE.UNKNOWN;
                Enum.TryParse(country_code, out convertResult);

                Console.WriteLine("string variable: " + Program.GetEnumDescription(convertResult));

                Console.ReadLine();

            }

            /// <summary>
            /// GET string description
            /// </summary>
            /// <param name="en"></param>
            /// <returns></returns>
            public static string GetEnumDescription(Enum en)
            {
                Type type = en.GetType();

                try
                {
                    MemberInfo[] memInfo = type.GetMember(en.ToString());

                    if (memInfo != null && memInfo.Length > 0)
                    {
                        object[] attrs = memInfo[0].GetCustomAttributes(typeof(EnumDisplayString), false);

                        if (attrs != null && attrs.Length > 0)
                            return ((EnumDisplayString)attrs[0]).DisplayString;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                return en.ToString();

            }
        }



        public enum COUNTRY_CODE
        {
            [EnumDisplayString("AFGHANISTAN")]
            AF
            ,
            [EnumDisplayString("ALBANIA")]
            AL
           ,
            [EnumDisplayString("UNKNOWN")]
            UNKNOWN
        }

        public class EnumDisplayString : Attribute
        {
            public string DisplayString;

            public EnumDisplayString(string text)
            {
                this.DisplayString = text;
            }
        }
    }

Upvotes: 1

Bruno Warmling
Bruno Warmling

Reputation: 400

Easy

public string Convert(string country)
    {
        string result = string.Empty;
        FieldInfo fieldInfo = GetType().GetField(country);
        result = fieldInfo?.GetValue(this)?.ToString();
        return result;
    }

Upvotes: 1

Related Questions