By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

Converting string or int to an Enum

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP***********************@news.microsoft.com. .. Ozzy Knox <oz******@freemail.absa.co.za> wrote:
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.


Sure. For ints, you just need to cast. For strings, you need
Enum.Parse. For example:

using System;

public class Test
{
enum Foo
{
Bar=2,
Baz=3
}

static void Main()
{
Foo x = (Foo)2;
Foo y = (Foo) Enum.Parse(typeof(Foo), "Baz");
Foo z = (Foo)4;

Console.WriteLine ("{0} {1} {2}", x, y, z);
}
}

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #2

P: n/a
Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote:
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 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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #3

P: n/a
Jon,
However, they are much more typesafe than VB6's implementation. ;-)
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

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ... Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote:
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 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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.