DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Tue, 14 Aug 2007 14:31:09 +0200,    posted on: microsoft.public.dotnet.languages.vc        back       

Thread Index
  1    Harald
          2    Harald
          3    Ben Voigt [C++ MVP] am


converting string in enum !!!   
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
Date:Tue, 14 Aug 2007 14:31:09 +0200   Author:  

Re: converting string in enum !!!   
Sorry wanted to put this in the csharp group. Please ignore!
-hd
Date:Tue, 14 Aug 2007 14:37:23 +0200   Author:  

Re: converting string in enum !!!   
"Harald"  wrote in message 
news:u0zmE8m3HHA.4584@TK2MSFTNGP03.phx.gbl...

> 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???


System.Enum.TryParse


>
> 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
> 
Date:Tue, 14 Aug 2007 10:18:46 -0500   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us