If this is ISO country code, please see:
http://en.wikipedia.org/wiki/Country_code[
^].
This is a less complete list:
http://countrycode.org/[
^].
You can find country code information for each country in alphabetical lists referenced in this article.
For convenience, I would first defined a list of countries as a enumeration type:
enum Country = {
Afghanistan = 4,
Åland_Islands = 248,
Albania = 8,
Algeria = 12,
}
The integer values here are ISO 3166-1 values:
http://en.wikipedia.org/wiki/ISO_3166-1[
^],
http://en.wikipedia.org/wiki/ISO_3166[
^].
If this is all you need, this would be enough. You can populate your list control from the data found in this enumeration type. You can find how write the loop iterating enumeration values from my article:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^].
There are some problems. If you need to carry more information in each country, you would need to find it in some internal data collection. The most convenient way is to have just the array of data indexed by the enumeration member. But how to do it? You will find the answer in the same article referenced above.
Another problem is this: some country names are not valid .NET identifiers, mostly because they contain blank spaces. For example, I've written "Åland Islands" above using underscore. This is not good for the UI. Of course, you can replace underscores before population of the UI with blank spaces, but this is a lame approach. The radical approach is shown in my other article:
Human-readable Enumeration Meta-data[
^].
Not only this method provides for human-readable names associated with enumeration members, it also provides a way to globalize the names of the country, so the UI can be localized to show these names in different cultures.
You can also use alternative or additional approach. First of all, in all cases it's good to load country information from some data file you store on the server. When you read this file, you store the data in a collection you populate from data. This is especially useful if you need to store some country-related information, not just country name and code, which is very likely. I would suggest to use
System.Collections.Generic.Dictionary<key, value>
. Please see:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[
^].
The type
value
would be the data class used to carry all information you need relevant to each country, and the
key
type should be the index of a country in a dropdown list, so you can use
System.Int32
, or
System.UInt32
. This way, you can easily find required record by the position of selection in the list. You may need to index this country information by another key, such as country code.
Make sure, that you create two or more related dictionaries correctly. First of all, make sure your
value
type is a reference, not a value type (above, I said "class", not "structure", to have a reference type). Make sure the two or more dictionaries share the same values. These way, you will have only one object representing a country referenced from two or more different collections.
—SA