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. Get number used for Enum

Get number used for Enum

Scheduled Pinned Locked Moved C#
question
14 Posts 6 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.
  • C Offline
    C Offline
    codemunkeh
    wrote on last edited by
    #1

    Got an enum. Got 2 get methods for it. One returns the instance of the enum itself, one intends to return the int used in the enum.

    public enum elementTypes
    {
    normalText = 0,
    hidden = 1,
    programData = 2,
    }

    There are more but this is an idea. As I said I'd like to be able to get the actual number, not the name or an instance.


    Ninja (the Nerd)
    Confused? You will be...

    K 1 Reply Last reply
    0
    • C codemunkeh

      Got an enum. Got 2 get methods for it. One returns the instance of the enum itself, one intends to return the int used in the enum.

      public enum elementTypes
      {
      normalText = 0,
      hidden = 1,
      programData = 2,
      }

      There are more but this is an idea. As I said I'd like to be able to get the actual number, not the name or an instance.


      Ninja (the Nerd)
      Confused? You will be...

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      The only way I know of doing this is: elementTypes et; //This will give you the number (Int32)et.hidden; Hope that helps. Ben

      C M 2 Replies Last reply
      0
      • K kubben

        The only way I know of doing this is: elementTypes et; //This will give you the number (Int32)et.hidden; Hope that helps. Ben

        C Offline
        C Offline
        codemunkeh
        wrote on last edited by
        #3

        Fantastic. Out of lack of skills, what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?


        Ninja (the Nerd)
        Confused? You will be...

        K G 2 Replies Last reply
        0
        • C codemunkeh

          Fantastic. Out of lack of skills, what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?


          Ninja (the Nerd)
          Confused? You will be...

          K Offline
          K Offline
          kubben
          wrote on last edited by
          #4

          Right. It casts the Enum as a number. It is kind of silly, but it the only way I know of to get the enums numerical value. I am sure someone else will post a different way of doing it, but that is the way I normally do it. Ben

          C A 2 Replies Last reply
          0
          • K kubben

            Right. It casts the Enum as a number. It is kind of silly, but it the only way I know of to get the enums numerical value. I am sure someone else will post a different way of doing it, but that is the way I normally do it. Ben

            C Offline
            C Offline
            codemunkeh
            wrote on last edited by
            #5

            OK... Thanks.


            Ninja (the Nerd)
            Confused? You will be...

            1 Reply Last reply
            0
            • K kubben

              The only way I know of doing this is: elementTypes et; //This will give you the number (Int32)et.hidden; Hope that helps. Ben

              M Offline
              M Offline
              MCEdwards
              wrote on last edited by
              #6

              I think a slightly better solution in this case is to use the int type rather than the Int32 object. I have not really used the Int32 object but I assume that it acts in the same way as other objects in that it passes itself by reference instead of value. This may then catch out other developers who are used to integers being value types and not objects. Instead use: TestEnum te = new TestEnum(); int enumNumber = (int) te; Hope this helps.

              G 1 Reply Last reply
              0
              • C codemunkeh

                Fantastic. Out of lack of skills, what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?


                Ninja (the Nerd)
                Confused? You will be...

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Ninja-the-Nerd wrote:

                what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?

                No, it's an explicit conversion. An implicit conversion is when the conversion is done without specifying the data type, this is allowed when widening types, like from a byte to an int. Example: byte data = 42; int moreData = data; An explicit conversion is when you specify the data type. This is used to tell the compiler that you want the conversion eventhough the compiler doesn't know if it will always work, or when an implicit conversion isn't allowed, like with an enum. Example: int data = 42; byte lessData = (byte)data;

                --- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams

                1 Reply Last reply
                0
                • M MCEdwards

                  I think a slightly better solution in this case is to use the int type rather than the Int32 object. I have not really used the Int32 object but I assume that it acts in the same way as other objects in that it passes itself by reference instead of value. This may then catch out other developers who are used to integers being value types and not objects. Instead use: TestEnum te = new TestEnum(); int enumNumber = (int) te; Hope this helps.

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  MCEdwards wrote:

                  I think a slightly better solution in this case is to use the int type rather than the Int32 object.

                  There is no difference.

                  MCEdwards wrote:

                  I have not really used the Int32 object

                  Yes, you have. The int keyword is an alias for the System.Int32 data type.

                  MCEdwards wrote:

                  I assume that it acts in the same way as other objects in that it passes itself by reference instead of value.

                  Assume away... ;) Int32 is a structure, not a class, so it's a value type, not a reference type.

                  --- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams

                  1 Reply Last reply
                  0
                  • K kubben

                    Right. It casts the Enum as a number. It is kind of silly, but it the only way I know of to get the enums numerical value. I am sure someone else will post a different way of doing it, but that is the way I normally do it. Ben

                    A Offline
                    A Offline
                    Anthony Mushrow
                    wrote on last edited by
                    #9

                    Yeah here's another way: int number = enum as int; XD

                    My current favourite word is: Waffle Cheese is still good though.

                    L 1 Reply Last reply
                    0
                    • A Anthony Mushrow

                      Yeah here's another way: int number = enum as int; XD

                      My current favourite word is: Waffle Cheese is still good though.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      The Undefeated wrote:

                      int number = enum as int;

                      that will not be accepted, for one "as" requires a reference type, not a value type. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                      A 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        The Undefeated wrote:

                        int number = enum as int;

                        that will not be accepted, for one "as" requires a reference type, not a value type. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                        A Offline
                        A Offline
                        Anthony Mushrow
                        wrote on last edited by
                        #11

                        *shakes fist angrily at Luc* You ruin everything :rolleyes: :rose: int Waffle(ref Enum myEnum) { return myEnum as int; }

                        My current favourite word is: Waffle Cheese is still good though.

                        L 1 Reply Last reply
                        0
                        • A Anthony Mushrow

                          *shakes fist angrily at Luc* You ruin everything :rolleyes: :rose: int Waffle(ref Enum myEnum) { return myEnum as int; }

                          My current favourite word is: Waffle Cheese is still good though.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          no no. Error: The as operator must be used with a reference type ('int' is a value type) :suss:

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                          A 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            no no. Error: The as operator must be used with a reference type ('int' is a value type) :suss:

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                            A Offline
                            A Offline
                            Anthony Mushrow
                            wrote on last edited by
                            #13

                            *cries* Perhaps if i actually tried to use the code before i posted it. *cries some more*

                            My current favourite word is: Waffle Cheese is still good though.

                            L 1 Reply Last reply
                            0
                            • A Anthony Mushrow

                              *cries* Perhaps if i actually tried to use the code before i posted it. *cries some more*

                              My current favourite word is: Waffle Cheese is still good though.

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              Yeah, the sensible sequence is: - try to understand the problem at hand - read the documentation - try a couple of things - search for a solution for the remaining problems - only then post at CodeProject :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                              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