Casting byte to Enum (Please help. Driving me nuts)

  • Thread starter The Last Gunslinger
  • Start date
T

The Last Gunslinger

We have an enum:
public enum EnumDays
{
Sun = 1,
Mon = 2,
...
}

We can store it as a byte:

byte ByteEnum = (byte)EnumDays.Sun;

We can cast it back to EnumDays:

EnumDays Days = (EnumDays)ByteEnum;

But we cannot Convert it using ChangeType:

Object EnumObject = Convert.ChangeType(ByteEnum, typeof(EnumDays));

Results in an InvalidCastException: Cannot convert from system.byte to
namespace.EnumDays.

Why does this not work.

I need to be able to do this so I can set a property of type EnumDays on
a class using reflection.

Any help much appreciated.

Cheers
JB
:(
 
J

Joe Mayo [C# MVP]

Hi JB,

How about this:

EnumDays day = (EnumDays)Enum.Parse(typeof(EnumDays), byteEnum.ToString());

Joe
 
T

The Last Gunslinger

Joe said:
Hi JB,

How about this:

EnumDays day = (EnumDays)Enum.Parse(typeof(EnumDays), byteEnum.ToString());

Hi Joe
Thanks, that works beautifully with a slight modification.

Since I am using reflection and I dont know the type beforehand I do
this and it works beautifully:

object Obj = Enum.Parse(F.GetType(), ByteEnum.ToString());
Class.GetType().GetProperty("Prop").SetValue(Class, Obj, null);

Thanks for your help.

Cheers
JB
:)
 
J

Jay B. Harlow [MVP - Outlook]

JB,
In addition to Joe's comments, I would consider using Enum.ToObject instead
of Enum.Parse (you save a string allocation).

EnumDays day = (EnumDays)Enum.ToObject(typeof(EnumDays), byteEnum);

-or-

object Obj = Enum.ToObject(F.GetType(), ByteEnum);
Class.GetType().GetProperty("Prop").SetValue(Class, Obj, null);

Enum.ToObject is overloaded for all the "integer" types & object.

Hope this helps
Jay
 
T

The Last Gunslinger

Jay said:
JB,
In addition to Joe's comments, I would consider using Enum.ToObject instead
of Enum.Parse (you save a string allocation).

EnumDays day = (EnumDays)Enum.ToObject(typeof(EnumDays), byteEnum);

-or-

object Obj = Enum.ToObject(F.GetType(), ByteEnum);
Class.GetType().GetProperty("Prop").SetValue(Class, Obj, null);

Enum.ToObject is overloaded for all the "integer" types & object.

Hope this helps
Jay

Excellent.

Thanks
JB :)
I still dont understand why Convert.ChangeType doesnt work.
 
J

Jay B. Harlow [MVP - Outlook]

JB,
I still dont understand why Convert.ChangeType doesnt work.
My understanding is that Convert.ChangeType requires the type to support
IConvertible. Looking at IConvertible there is no method for "ToEnum".

In other words you can use IConvertible to convert from an Enum, but not to
an Enum.

Hope this helps
Jay
 
T

The Last Gunslinger

Jay said:
JB,


My understanding is that Convert.ChangeType requires the type to support
IConvertible. Looking at IConvertible there is no method for "ToEnum".

In other words you can use IConvertible to convert from an Enum, but not to
an Enum.

Hope this helps
Jay

Cheers
JB
Going off to read the doco again :)
 
G

Guest

Hi,

If "EnumDays" is your class and you can change it, so
i can propose this:

public enum EnumDays: byte {
Sun = 1,
Mon = 2,
// ...
}

then you will able to do it:

EnumDays day=EnumDays.Mon;
byte byteDay=(byte) day;
EnumDays tempDay=(EnumDays) byteDay;

Cheers!

Marcin
 

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