Converting Enums (c#)

M

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
 
L

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>
 
M

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
 

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

Top