-1

My programm locates your IP Adress with country. when you are from germany, it will display DE when you are in USA it will display US etc.

Now my question is how to turn automaticly the string maybe from HU to Hungary. Should i create an itemlist or sth like that?

Screenshot

There you see, I'm getting the country information from a website http://api.hostip.info/country.php You see above the Get IP button Country: DE and i want to change this automaticly in Germany

tshepang
  • 12,111
  • 21
  • 91
  • 136
Diar Do
  • 11
  • 1
  • 2
    Probably a dictionary. The full list is [here](http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm) – Mike Christensen Aug 02 '13 at 00:00

3 Answers3

1

You could create a dictionary and set the key to be your returned result with the value to be the desired country name. Look here for a basic overview: Tutorial

JoeCo
  • 695
  • 6
  • 13
1

Here are two options for solving this issue:

  • Build a Dictionary<Country, string>
  • Use the Country enumeration and decorate it with a Description attribute

Build a Dictionary<Country, string>, like this:

enum Country
{
    UnitedStates,
    Germany,
    Hungary
}

Dictionary<Country, string> CountryNames = new Dictionary<Country, string>
{
    { Country.UnitedStates, "US" },
    { Country.Germany, "DE" }
    { Country.Hungary, "HU" }
};

static string ConvertCountry(Country country) 
{
    string name;
    return (CountryNames.TryGetValue(country, out name))
        ? name : country.ToString();
}

Now you can use the Dictionary<Country, string> via the static ConvertCountry method, like this:

var myCountry = ConvertCountry(Country.UnitedStates));

Use the Country enumeration and decorate it with a Description attribute, like this:

enum Country
{
    [Description("US")]
    UnitedStates,
    [Description("DE")]
    Germany,
    [Description("HU")]
    Hungary
}

Now you can use this method to get a Description attribute value, like this:

public static string GetDescription(Enum en)
    {
        Type type = en.GetType();

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

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

            if (attrs != null && attrs.Length > 0)
            {
                return ((DescriptionAttribute)attrs[0]).Description;
            }
        }

        return en.ToString();
    }

Usage of above method, like this:

var myCountryDescription = GetDescription(Country.UnitedStates);
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
1

You could create a object that relations the Short and the long country name using this table.

That´s also another post that answer this post

Community
  • 1
  • 1