Converting string or int to an Enum

  • Thread starter Thread starter Ozzy Knox
  • Start date Start date
O

Ozzy Knox

I am trying to convert a string or int variable to an Enum that I have set
up in my C# application. I have tried many options with no success. Any
advice would be appreciated.

Regards
Ozzy
 
Jon,
Note how here, z *is* a Foo, but it's not one of the "predefined"
members of the enumeration. Just wanted to point out that enums aren't
as type-safe as you might hope.
PMFJI: I totally agree!

However, they are much more typesafe than VB6's implementation. ;-)

I also prefer them over the constant integers in Java classes.

Just a thought
Jay
 
Jay B. Harlow said:
Jon,
PMFJI: I totally agree!

However, they are much more typesafe than VB6's implementation. ;-)

I don't know VB6, but I wouldn't be surprised :)
I also prefer them over the constant integers in Java classes.

Note that Java is getting enums in Java 1.5, too... I believe they're
going to be more typesafe than these, but I haven't looked at the
details yet.
 
Jon,
I don't know VB6, but I wouldn't be surprised :)

VB6 allows (in C# like syntax):
Foo x = 2; // no requirement to cast the enum
Foo z = Baz; // no requirement to prefix the enum

Neither of which I care for.

I've been researching Code Critics. I think identifying casts on enums
should be flagged as a warning, especially when the number is outside the
range of values.

Code Critics should at the very least flag the following line:
Foo z = (Foo)4;

It should be interesting to see how typesafe they make Java's. And if that
could be adopted to .NET. :-)

Jay
 
Back
Top