Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

enum basics

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rodchar - 30 Jan 2008 14:43 GMT
hey all,
is there anyway to ask the Enum what the string value is with a given value?
Like how you would access an element of an array?

enum Myenum
{
test1=1
test2=2
}

This won't work but may better explain:
String a = MyEnum[1];

thanks,
rodchar
gerry - 30 Jan 2008 14:59 GMT
MyEnum.test1.ToString( );

or

Enum.GetName ( typeof ( MyEnum ) , MyEnum.test1 )

or

Enum.GetNames ( typeof ( MyEnum ) ) [ 0 ]

> hey all,
> is there anyway to ask the Enum what the string value is with a given
[quoted text clipped - 12 lines]
> thanks,
> rodchar
Jeroen - 30 Jan 2008 15:13 GMT
What is "none of the above", Alex?

All options will cause you problems if you obfuscate your code, found
that one out the hard way ;-).

Regards,
Jeroen

PS. If this thread was meant as a particular question, then I missed
it...
Ignacio Machin ( .NET/ C# MVP ) - 30 Jan 2008 21:11 GMT
Hi,

Could you elaborate in what happened to you?

thanks,

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> What is "none of the above", Alex?
>
[quoted text clipped - 6 lines]
> PS. If this thread was meant as a particular question, then I missed
> it...
Alun Harford - 30 Jan 2008 21:21 GMT
> What is "none of the above", Alex?
>
> All options will cause you problems if you obfuscate your code, found
> that one out the hard way ;-).

Hmm... then somewhere you needed to map those enums to strings, which
suggests the obfuscation was totally pointless anyway. As per usual.

Alun Harford
rodchar - 30 Jan 2008 15:15 GMT
Thanks Gerry, i appreciate it.
rod.

> MyEnum.test1.ToString( );
>
[quoted text clipped - 22 lines]
> > thanks,
> > rodchar
Ben Voigt [C++ MVP] - 30 Jan 2008 21:33 GMT
> MyEnum.test1.ToString( );

ok, but you specified the enum element by name already

> or
>
> Enum.GetName ( typeof ( MyEnum ) , MyEnum.test1 )

ok, but you specified the enum element by name again

> or
>
> Enum.GetNames ( typeof ( MyEnum ) ) [ 0 ]

This returns the first name, not the name corresponding to the value (0).

>> hey all,
>> is there anyway to ask the Enum what the string value is with a given
[quoted text clipped - 12 lines]
>> thanks,
>> rodchar
Ben Voigt [C++ MVP] - 30 Jan 2008 21:32 GMT
> hey all,
> is there anyway to ask the Enum what the string value is with a given
[quoted text clipped - 9 lines]
> This won't work but may better explain:
> String a = MyEnum[1];

int i = 1;
((Myenum)i).ToString();

> thanks,
> rodchar

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.