Smart way to connect enums to string values

  • Thread starter Thread starter Henke
  • Start date Start date
H

Henke

Hi!
Has anyone a nice way to connect a enum to a string value.
Now I declare a const string [] in the same class as my enum is defined. But
I doubt that's the "object oriented" way of doing it.
Any better suggestions?

Thanks in advance?
/Henke
 
Henke said:
Hi!
Has anyone a nice way to connect a enum to a string value.
Now I declare a const string [] in the same class as my enum is defined.
But I doubt that's the "object oriented" way of doing it.
Any better suggestions?

Thanks in advance?
/Henke

I don't know if I understand your question well, but you might like to have
a look at the
Enum.ToString() and Enum.Parse() functions, like:

enum Test { One, Two, Three };

public static void Main(string[] args)
{
Test tst = Test.One;
string str = tst.ToString();
Test tst2 = (Test) Enum.Parse(typeof(Test), str);

Console.WriteLine("tst = {0}\nstr = {1}\ntst2 = {2}", tst, str, tst2);
}

HTH,
Stefan
 
If those names defined in the string array matches the names in the enum to
which they refer, just use ToString on the enum variable.
If not, have a look at the Description attribute, which you may apply to the
members of the enum
and obtain at runtime.
 
Yeh, that's ok as long as you want the same string as your enum-name. But
since I'm from sweden and code in english but want's my displayed enum
strings in swedish I can't do it that way.

Any other good ideas?
/Henke

©tefan ©imek said:
Henke said:
Hi!
Has anyone a nice way to connect a enum to a string value.
Now I declare a const string [] in the same class as my enum is defined.
But I doubt that's the "object oriented" way of doing it.
Any better suggestions?

Thanks in advance?
/Henke

I don't know if I understand your question well, but you might like to
have a look at the
Enum.ToString() and Enum.Parse() functions, like:

enum Test { One, Two, Three };

public static void Main(string[] args)
{
Test tst = Test.One;
string str = tst.ToString();
Test tst2 = (Test) Enum.Parse(typeof(Test), str);

Console.WriteLine("tst = {0}\nstr = {1}\ntst2 = {2}", tst, str, tst2);
}

HTH,
Stefan
 
Then use Description(System.ComponentModel.DescriptionAttribute):

enum MyEnum
{
[ Description ("Min beskrivning.") ]
Member1,
[ Description ("En annan beskrivning.") ]
Member2,
}



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Henke said:
Yeh, that's ok as long as you want the same string as your enum-name. But
since I'm from sweden and code in english but want's my displayed enum
strings in swedish I can't do it that way.

Any other good ideas?
/Henke

©tefan ©imek said:
Henke said:
Hi!
Has anyone a nice way to connect a enum to a string value.
Now I declare a const string [] in the same class as my enum is defined.
But I doubt that's the "object oriented" way of doing it.
Any better suggestions?

Thanks in advance?
/Henke

I don't know if I understand your question well, but you might like to
have a look at the
Enum.ToString() and Enum.Parse() functions, like:

enum Test { One, Two, Three };

public static void Main(string[] args)
{
Test tst = Test.One;
string str = tst.ToString();
Test tst2 = (Test) Enum.Parse(typeof(Test), str);

Console.WriteLine("tst = {0}\nstr = {1}\ntst2 = {2}", tst, str, tst2);
}

HTH,
Stefan
 
Henke,

When I want to do something like this, I attach the Description
attribute from the System.ComponentModel namespace to each of the
enumeration values. Then, when I want the name, I use a method (you will
have to code this yourself) which gets the value of the attribute from the
field (remember, each value is a static field on the class), and then gets
the value of the description from the attribute.

Of course, this can be modified to use any attribute, which you can then
use to point to a resource, if you wish.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henke said:
Yeh, that's ok as long as you want the same string as your enum-name. But
since I'm from sweden and code in english but want's my displayed enum
strings in swedish I can't do it that way.

Any other good ideas?
/Henke

©tefan ©imek said:
Henke said:
Hi!
Has anyone a nice way to connect a enum to a string value.
Now I declare a const string [] in the same class as my enum is defined.
But I doubt that's the "object oriented" way of doing it.
Any better suggestions?

Thanks in advance?
/Henke

I don't know if I understand your question well, but you might like to
have a look at the
Enum.ToString() and Enum.Parse() functions, like:

enum Test { One, Two, Three };

public static void Main(string[] args)
{
Test tst = Test.One;
string str = tst.ToString();
Test tst2 = (Test) Enum.Parse(typeof(Test), str);

Console.WriteLine("tst = {0}\nstr = {1}\ntst2 = {2}", tst, str, tst2);
}

HTH,
Stefan
 
I blogged about this a while back (unfortunately my content and the blog engine aren't too happy together so this link is to the .NET category, ut just look for the two posts on extending enums)

http://staff.develop.com/richardb/weblog/CategoryView.aspx/.NEt

I created my own attribute as I didn't want to be dependent on the System.ComponentModel but same principle. The blog entries have the code you'll need for going both ways between the enum and the alternative text.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

If those names defined in the string array matches the names in the enum to
which they refer, just use ToString on the enum variable.
If not, have a look at the Description attribute, which you may apply to the
members of the enum
and obtain at runtime.
 
Back
Top