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