Hi, I was wondering if it is possible to reverse the way my enum variables work
In our storage XML Files all the data is stored as integers, which I want to translate into strings using a lookup table. I thought enums would be perfect for this but I can't work out how to get the string from the integer

e.g.

enum sourceType
{
SIM = 0,
ME = 1,
MC = 2,
SATNAV = 3
}

I want to be able to turn the integer 1 into the string "ME". As far as I can tell I can only get the integer 1 by doing sourceType.ME but that's no use to me.

Regards

D

int i = (int)sourceType.ME;

this should make i=1

int i = (int)sourceType.ME;

this should make i=1

I don't need to get the value '1' I need to get the value 'ME' from the value '1'

What I'd ideally like to do is

string thisSourceType = sourceType.1

thisSourceType would then equal "ME"

This is not possible I guess.
You can do:
string thisSourceType = sourceType.ME.ToString();

Or you can try this:

int index = 1;
Array list = Enum.GetValues(typeof(sourceType));
string value = list.GetValue(index).ToString();

Mitja

It's a lot easier than that:

int a = 1;
sourceType b = (sourceType)a;

Any int can be cast into any enum. If it is valid, ToString() will show the text of the enum, otherwise you'll get the number. For example:

int a = 1;
sourceType b = (sourceType)a;
Console.WriteLine(b.ToString());   // Output is 'ME'
a = 100;
b = (sourceType)a;
Console.WriteLine(b.ToString());   // Output is '100'

Thanks peeps :-D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.