Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. enum to int

enum to int

Scheduled Pinned Locked Moved C#
questionhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lasse Johansen
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • L Lasse Johansen

      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

      A Offline
      A Offline
      Arun Bhalla
      wrote on last edited by
      #2

      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 returning null, perhaps because the enumString is incorrect?

      L 1 Reply Last reply
      0
      • A Arun Bhalla

        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 returning null, perhaps because the enumString is incorrect?

        L Offline
        L Offline
        Lasse Johansen
        wrote on last edited by
        #3

        Hehe.. Playing a little around.... System.Convert.ToInt32(enumObject) works perfect. Thanks anyway Gooky

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups