469,312 Members | 2,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Convert int to enum

I am writing general method that would take Type that should represent a
enum that do something with it.

In the middle of the method I need to cast it to this enum. I tried
Convert.ChangeType but it throws an exception - InvalidCast.

Wanted to know is it possible to make cast if you don't know the type at
the time of writing the code?

Thanks,
Shimon.
Nov 16 '05 #1
3 27219
Hi Shimon,

I think you need to read about the enum data type.. I don't see a way to
convert a int to enum directly... but I know...
Each enum member has an associated constant value, and you may assign a int
value to a enum

enum Page
{
Max = 200
}

(int)Page.Max

Please read http://www.jaggersoft.com/csharp_standard/11.1.8.htm

Nirosh.

"Shimon Sim" <es*****@att.net> wrote in message
news:eF**************@TK2MSFTNGP09.phx.gbl...
I am writing general method that would take Type that should represent a
enum that do something with it.

In the middle of the method I need to cast it to this enum. I tried
Convert.ChangeType but it throws an exception - InvalidCast.

Wanted to know is it possible to make cast if you don't know the type at
the time of writing the code?

Thanks,
Shimon.

Nov 16 '05 #2
Shimon Sim wrote:
I am writing general method that would take Type that should represent a
enum that do something with it.

In the middle of the method I need to cast it to this enum. I tried
Convert.ChangeType but it throws an exception - InvalidCast.

Wanted to know is it possible to make cast if you don't know the type at
the time of writing the code?

<snip>

What exactly do you need the "enum" for in the middle here ? Since you
can't write: EnumType eval = (EnumType)integerValue;

then I assume you want to do something like print it out, ie. the name
of the enum member instead of its integer value.

If that's what you want, you can do this:

Object value = Enum.ToObject(typeof(EnumType), integerValue);

and you can of course substitute "typeof(EnumType)" with any Type object
referring to an Enum type.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #3
"Champika Nirosh" <te**@test.lk> wrote in message news:uH**************@TK2MSFTNGP14.phx.gbl...
I think you need to read about the enum data type.. I don't see a way to
convert a int to enum directly...


Shimon could write,

Enum.Parse( myEnumVar.GetType( ), myIntVar.ToString( ) );

but under normal circumstances he must eventually write an explicit
typecast when making the assignment. The only possible way around
it might be to use Reflection to set the value on a field member if that
were what myEnumVar were. For example,

// . . .
public class EnumCasting
{
public MyEnum settings;

public static void SetArbitraryEnumType( Type enumType, int enumValue)
{
this.GetType( ).GetField( "settings",
BindingFlags.Instance | BindingFlags.Public).SetValue(
this,
Enum.Parse( enumType, enumValue.ToString( ))
);
}
}
// . . .

This way Shimon doesn't absolutely need to know the Type of the
field named "settings" (if a field with the same name, but different enum
type, existed on multiple subclasses that could all inherit the method's
implementation ... bwawahwahwah ... that was a mouthful.) I'm not
saying this is a very good practice for class manageability in many
cases -- just that it's possible. :-)
Derek Harmon
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gad Rosenthal | last post: by
4 posts views Thread by Nikhil Patel | last post: by
21 posts views Thread by Andreas Huber | last post: by
2 posts views Thread by Pavils Jurjans | last post: by
2 posts views Thread by Dennis | last post: by
3 posts views Thread by David A. Osborn | last post: by
5 posts views Thread by Barry | last post: by
13 posts views Thread by At_sea_with_C | last post: by
2 posts views Thread by Tim Sprout | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.