473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Smart way to connect enums to string values

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
Nov 16 '05 #1
7 2110

"Henke" <he********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
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
Nov 16 '05 #2
Try Enum.GetNames(typeof(yourenum)).

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Henke" <he********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #3
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.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Henke" <he********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #4
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" <si********@kascomp.blah.sk> skrev i meddelandet
news:ud**************@TK2MSFTNGP15.phx.gbl...

"Henke" <he********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #5
Then use Description(System.ComponentModel.DescriptionAttri bute):

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

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Henke" <he********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <si********@kascomp.blah.sk> skrev i meddelandet
news:ud**************@TK2MSFTNGP15.phx.gbl...

"Henke" <he********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
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


Nov 16 '05 #6
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]
- mv*@spam.guard.caspershouse.com

"Henke" <he********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <si********@kascomp.blah.sk> skrev i meddelandet
news:ud**************@TK2MSFTNGP15.phx.gbl...

"Henke" <he********@hotmail.com> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
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


Nov 16 '05 #7
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/we...View.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/<ts******************@news2.e.nsc.no>

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.
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
0
by: lglmi | last post by:
'********************START OF BAS MODULE******************** Option Explicit Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long...
0
by: TRUMPh | last post by:
Context: I have a list with indeterminately entrys @design time. The user should choose one of the item via property grid. I create a classes and a instances of enums @RunTime. For each...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
4
by: aschmidt | last post by:
Another bug in smart navigation. If smartnavigation is enabled in the page and session timeout occurs, the framework redirects you to wrong location of login.aspx page if you use Forms...
10
by: Jay | last post by:
In C# I can set up an Enum so that number are represented as keywords, which is very useful. Is there such a datatype in a database? I suppose I could use an extra table, with the ID column as...
3
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have a class as follows: class GamesConsole { public int iReference; public enum Maker {Nintendo, Sega, Sony, Panasonic} }
37
by: Ian Semmel | last post by:
If I have Enum en { i1, i2, i3 } I reckon I should be able to have
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.