Home All Groups Group Topic Archive Search About

Converting Enums (c#)

Author
25 Jan 2006 12:32 AM
Mark Olbert
Given:

    public enum SomeEnumType : int
    {
        None = 0,
    }

    int b = 0;

why does the following cast succeed:

    SomeEnumType a = (SomeEnumType) b;

while the following conversion fails:

    SomeEnumType a = Convert.ChangeType(b, typeof(SomeEnumType));

Is there a "generic" way of converting integer values into integer-based enums?

- Mark

Author
25 Jan 2006 12:47 AM
Lloyd Dupont
with a cast as in your first attempt?
if you really want to use some automatic/reflection way of doing that:
SomeEnumType a = (SomeEnumType)Enum.ToObject(typeof(SomeEnumType), b);

But granted I would take it as bug worth reporting there:
http://lab.msdn.microsoft.com/productfeedback/

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
Show quote
"Mark Olbert" <ChairmanMAO@newsgroups.nospam> wrote in message
news:6jhdt1h82sgdstktjoj17b6j9l3vpvjohr@4ax.com...
> Given:
>
> public enum SomeEnumType : int
> {
> None = 0,
> }
>
> int b = 0;
>
> why does the following cast succeed:
>
> SomeEnumType a = (SomeEnumType) b;
>
> while the following conversion fails:
>
> SomeEnumType a = Convert.ChangeType(b, typeof(SomeEnumType));
>
> Is there a "generic" way of converting integer values into integer-based
> enums?
>
> - Mark
Author
25 Jan 2006 1:13 AM
Mark Olbert
Thank you thank you thank you!! I don't know how I missed that method of Enum, but it neatly solves the problem!

- Mark

AddThis Social Bookmark Button