Enum to represent string??

  • Thread starter Thread starter guoqi zheng
  • Start date Start date
G

guoqi zheng

We can enum to represent a interger using a string. But how can we use enum
to represent string

below is what I want to achieve.

Public Enum country
Netherlands = "nl"
America = "US"
France = "fr"
End Enum

Obviously above code does not work. How can I achieve a similiar function
like above???

regards,

Guoqi Zheng
http://www.ureader.com
 
guoqi said:
We can enum to represent a interger using a string. But how can we
use enum to represent string

below is what I want to achieve.

Public Enum country
Netherlands = "nl"
America = "US"
France = "fr"
End Enum

Obviously above code does not work. How can I achieve a similiar
function like above???

regards,

Guoqi Zheng
http://www.ureader.com

What you can try to simulate this:
use a class ("country"), that contains (only) a set of string constants.

In C#:
public class Country
{
public const string Netherlands = "nl";
public const string America = "us";
public const string France = "fr";
}

Hans Kesting

}
 
It's not quite as readable as the actual country name, but you could also do
this:
Public Enum Country
nl,
US,
fr
End Enum

....if you need the string value, you can then do, for example,
Country.nl.ToString().
The advantage over using a Class is that it's strongly typed.
The disadvantage is that you need to know the country code rather than the
name when you develop.

HTH,
R.
 
Back
Top