enum to int
-
Hello I have an object. This object contains an enum value, but I do not know which type the value is. I can find out by asking GetType. The problem is that the object can contain different kinds of enums. All these enums inherit from int, and I want to know the value of this int. How do I get the value of an object, when I know that the object contains an enum value of type GetType? Programically(pseudo code), it can be written like: intGetIntFromEnum(Type type, string enumString) { object enumObject = Enum.Parse(type, enumString); return (int)enumObject; // Here it fails..., even though the enumobject's type is inheriting from int } Gooky
-
Hello I have an object. This object contains an enum value, but I do not know which type the value is. I can find out by asking GetType. The problem is that the object can contain different kinds of enums. All these enums inherit from int, and I want to know the value of this int. How do I get the value of an object, when I know that the object contains an enum value of type GetType? Programically(pseudo code), it can be written like: intGetIntFromEnum(Type type, string enumString) { object enumObject = Enum.Parse(type, enumString); return (int)enumObject; // Here it fails..., even though the enumobject's type is inheriting from int } Gooky
That should work! I use very similar code in a custom XML serializer/deserializer I wrote. This also works:
MessageBox.Show(((int)Enum.Parse(typeof(AnchorStyles), "Left")).ToString());
It displays "4". Maybe enumObject is returningnull
, perhaps because the enumString is incorrect? -
That should work! I use very similar code in a custom XML serializer/deserializer I wrote. This also works:
MessageBox.Show(((int)Enum.Parse(typeof(AnchorStyles), "Left")).ToString());
It displays "4". Maybe enumObject is returningnull
, perhaps because the enumString is incorrect?Hehe.. Playing a little around.... System.Convert.ToInt32(enumObject) works perfect. Thanks anyway Gooky