Enum to represent string??

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
 
H

Hans Kesting

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

}
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top