Converting string or int to an Enum

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
J

Jon Skeet

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.
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enum TypeConverter 3
enum with underlying type 1
Enum Extentions 7
Enums & Constructors? 4
Enum Question 1
I have small probalem 4
Casting in a generic function 3
convert DataRow col value to enum type 1

Top