|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Converting Enums (c#)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 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/ -- Show quoteRegards, Lloyd Dupont NovaMind development team NovaMind Software Mind Mapping Software <www.nova-mind.com> "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 |
|||||||||||||||||||||||