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 / Managed C++ / August 2007

Tip: Looking for answers? Try searching our database.

converting string in enum !!!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Harald - 14 Aug 2007 13:31 GMT
Hi, sorry if this is not the best group for this...

In C# I have string values that correspond to enum member names. I need a
conversion from the string value to the corresponding enum value - if
possible without a big switch comparing the string with all their enum
string literal representations. Thougth, there must be a trick with
reflection or so. Anyone an idea???

Sample (pseudo code):

enum color { red, green blue };
string myCol = "red";

try {
color Receiver = (color)myCol;    // this is erroneous because the cast is
not possible, but thats what I need :-)
}
catch {
...
}

Thx for all replies!
-hd
Harald - 14 Aug 2007 13:37 GMT
Sorry wanted to put this in the csharp group. Please ignore!
-hd
Ben Voigt [C++ MVP] - 14 Aug 2007 16:18 GMT
> Hi, sorry if this is not the best group for this...
>
[quoted text clipped - 3 lines]
> string literal representations. Thougth, there must be a trick with
> reflection or so. Anyone an idea???

System.Enum.TryParse

> Sample (pseudo code):
>
[quoted text clipped - 11 lines]
> Thx for all replies!
> -hd

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.