Get RegionInfo by country name?
Asked Answered
K

6

15

I want to be able to get RegionInfo by doing the following:

new RegionInfo("United Kingdom");

but this throws an exception and says that it is not recognised.

This page on RegionInfo says that an exception is thrown if 'name is not a valid country/region name'.

And yet this page specifies a list of predefined regions used by the class that and contains United Kingdom, so why doesn't creating a new RegionInfo with country name work?

Kirkwall answered 10/1, 2013 at 16:1 Comment(1)
Please, read the documentation well: The RegionInfo name is one of the two-letter codes defined in ISO 3166 for country/region.Clementia
L
36
  var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
  var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains(name));

If you want to get RegionInfo by the country name, you could get an IEnumerable<RegionInfo> and then filter based on the EnglishName as above. This gives you the ability to populate things such as comboboxes too.

Lyonnais answered 10/1, 2013 at 16:14 Comment(4)
But it would be null since the EnglishName is "English (United Kingdom)" ;-) So you might want to use FirstOrdefault(r => r.EnglishName.Contains(name))Mayer
@LukeHennerley: It will be still null but now without an exception. +1 anyway since Contains instead of == would be too generous.Mayer
It works for me, EnglishName is appearing as "United Kingdom"Kirkwall
@Tyler I thought it would work too, the english name for GB on the MSDN is simply United KingdomLyonnais
F
3

That same page you linked also says:

The RegionInfo name is one of the two-letter codes defined in ISO 3166 for country/region. Case is not significant; however, the Name, the TwoLetterISORegionName, and the ThreeLetterISORegionName properties return the appropriate code in uppercase.

The codes are on the page, and GB appears to be the 2 letter code for the UK (it's in code order to be difficult searching!). So try this:

new RegionInfo("GB");

Or if you're using .NET 2.0+, it's recommended you use the full culture name:

new RegionInfo("en-GB");
Flowerpot answered 10/1, 2013 at 16:6 Comment(2)
The Original Poster linked an old version of the spec. Since .NET 2.0 it is recommended to use e.g. "en-GB" instead of just "GB".Tyika
Great. The other possibilities with GB are: Welsh (United Kingdom) "cy-GB" and Scottish Gaelic (United Kingdom) "gd-GB".Tyika
V
2

From MSDN;

A string that contains a two-letter code defined in ISO 3166 for country/region.

UNITED KINGDOM looks ok on Country names and code elements on the ISO website.

GB UNITED KINGDOM

Try with;

new RegionInfo("GB");
Various answered 10/1, 2013 at 16:6 Comment(1)
The Original Poster linked an old version of the spec. Since .NET 2.0 it is recommended to use e.g. "en-GB" instead of just "GB".Tyika
G
1

If I navigate to the constructor the summary I see in Visual Studio says:

name: A string that contains a two-letter code defined in ISO 3166 for country/region.-or-A string that contains the culture name for a specific culture, custom culture, or Windows-only culture. If the culture name is not in RFC 4646 format, your application should specify the entire culture name instead of just the country/region.

The entire culture name would be 'en-GB'.

Or you could use 'GB'

Genevivegenevra answered 10/1, 2013 at 16:9 Comment(1)
There's a newer version of the doc page compared to what the original question links to. It is clearly recommended to use a full specific culture, like "en-GB", instead of just "GB". We don't know what version of the framework is used by the Original Poster, but he should certainly go with new RegionInfo("en-GB").Tyika
A
0

Look at the MSDN page:

A string containing one of the two-letter codes defined in ISO 3166 for country/region.

You need the ISO 3166 code for the UK, not the name of the country.

Here's the code you need.

Amphicoelous answered 10/1, 2013 at 16:5 Comment(0)
T
0

Note this comment from the metadata for the parameter name which explains the change from .NET Framework 2.0 on:

    //     A string containing one of the two-letter codes defined in ISO 3166 for country/region.-or-Beginning
    //     in .NET Framework version 2.0, a string containing the culture name for a
    //     specific culture, custom culture, or Windows-only culture. If the culture
    //     name is not in RFC 4646 format, your application should specify the entire
    //     culture name, not just the country/region.
Thing answered 10/1, 2013 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.