Reputation: 13426
I have C# code which can generate a list like this, using Cultures (link):
AUD $
CAD $
EUR €
GBP £
JPY ¥
USD $
What I want is a list like this too:
Australian Dollar
Canadian Dollar
Euro
British Pound
Japanese Yen
United States Dollar
What I actually want is that there should be a drop-down style Winforms Combobox, from which the user selects the currency, such as British Pound
, and then through a function or something, GBP
or £
gets returned. This will help me parse the currency value the user might type in the adjacent currency value text box.
So can I use Cultures (or something else which does not require an Internet connection) to get a descriptive Currency Names list ?
Upvotes: 3
Views: 6462
Reputation: 811
If you're using the full .NET Framework,* you can fetch the unique currencies by enumerating through the CultureInfo
instances and creating the related RegionInfo
by LCID
. You can then return the relevant currency fields from RegionInfo
, namely, ISOCurrencySymbol
, CurrencyEnglishName
, and CurrencySymbol
.
For example, the following code will return the distinct ISOCurrencySymbol
along with the associated name and symbol.
var currencies = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(ci => ci.LCID).Distinct()
.Select(id => new RegionInfo(id))
.GroupBy(r => r.ISOCurrencySymbol)
.Select(g => g.First())
.Select(r => new
{
r.ISOCurrencySymbol,
r.CurrencyEnglishName,
r.CurrencySymbol,
});
For the ISO Symbols provided in the original question, the following information would be returned:
AUD, $, Australian Dollar
CAD, $, Canadian Dollar
EUR, ?, Euro
GBP, £, British Pound
JPY, ¥, Japanese Yen
USD, $, US Dollar
Note: RegionInfo.CurrencyNativeName
also exists, but it will provide the currency name in the native language of the given region. Thus, the same ISOCurrencySymbol
may have various instances of CurrencyNativeName
associated with it and would not prove useful for mapping in this scenario.
* .NET Core does not appear to support enumerating CultureInfo
.
Upvotes: 3
Reputation: 13600
In my opinion the easiest way to accomplish this would be to create a wrapper class for the Currency, something like this:
public class Currency
{
public string Code { get; set; } // USD
public string Name { get; set; } // United States Dollar
public string Symbol { get; set; } // $
}
You can create a collection of these classes and set Name
as a member to display in your combobox. After the user selects some value, you can easily use Linq for instance to get the correspondent class.
Upvotes: 0