473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
:(
Nov 16 '05 #1
7 51237
Hi JB,

How about this:

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

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com

"The Last Gunslinger" <jb******@yahoo.com> wrote in message
news:82*****************@news-server.bigpond.net.au...
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
:(

Nov 16 '05 #2
Joe Mayo [C# MVP] wrote:
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
:)
Nov 16 '05 #3
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
"The Last Gunslinger" <jb******@yahoo.com> wrote in message
news:82*****************@news-server.bigpond.net.au...
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
:(

Nov 16 '05 #4
Jay B. Harlow [MVP - Outlook] wrote:
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.
Nov 16 '05 #5
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

"The Last Gunslinger" <jb******@yahoo.com> wrote in message
news:nA******************@news-server.bigpond.net.au... Jay B. Harlow [MVP - Outlook] wrote:
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.

Nov 16 '05 #6
Jay B. Harlow [MVP - Outlook] wrote:
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


Cheers
JB
Going off to read the doco again :)
Nov 16 '05 #7
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
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
:(

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: matt.sus | last post by:
This has been driving me crazy for days: IF ANYONE CAN HELP FIX THE HTML BELOW, PLEASE HELP! (please email me at matt.sus@gmail.com - or reply here) i am trying to load up a random background...
3
by: dd_bdlm | last post by:
Please help this one is driving me mad! I have searched and read all the topics on the error message I am receiving but none seem to apply to me! I have quite a complex query linking all parts...
2
by: nitin8or | last post by:
Hi ALL, I want to display in a RichTextBox the Binary Large Objects data coming from database. If I have one record its not a problem I convert it to byte array and pass it on in a stream as a...
1
by: A Hirsi | last post by:
I have created a vb .net program as a service that is using a simple ftpclient to connect to a remote server and check the status of a file for subsequent downloading if there have been changes to...
1
by: kfc1976 | last post by:
Hi All, First of all, following are the facts 1) I've been trying to solve the following issue for the past 8 hours (Not Kidding) 2) If I can't find the answer, It will drive me completely crazy...
3
by: jegray | last post by:
I am very much a beginner in dealing with connection statments. I am getting the following error: Microsoft OLE DB Provider for ODBC Drivers error '80004005' Data source name not found and...
0
by: jegray | last post by:
I am very much a beginner in dealing with connection statments. I am getting the following error: Microsoft OLE DB Provider for ODBC Drivers error '80004005' Data source name not found and no...
0
by: jegray | last post by:
I am very much a beginner in dealing with connection statments. I am getting the following error: Microsoft OLE DB Provider for ODBC Drivers error '80004005' Data source name not found and no...
1
by: Willow | last post by:
Hi I hope you can help me with this it is driving me nuts. I've created a database which I needed to give multiple people access to but didn't want them messing so I created an MDE file. I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.