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. Getting Enum Type from Value

Getting Enum Type from Value

Scheduled Pinned Locked Moved C#
jsonquestionlearning
4 Posts 3 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.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    Alright, I have an enum called Suit ... public enum Suit { Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3, Null = -1, } And I have a function I need to create that will return the correct rank type given an integer. I have written this function to work with a switch statement: switch(i) { case 0: return hearts; ... } BUT, there must be a much cleaner way to do this, by parsing the enum type from the value - I just don't know the syntax and can't seem to find it. It must be something like: Rank r = Enum.Parse(Rank, i); ... of course that doens't work. But it must be something similar. Any suggestions?

    L S 2 Replies Last reply
    0
    • B budidharma

      Alright, I have an enum called Suit ... public enum Suit { Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3, Null = -1, } And I have a function I need to create that will return the correct rank type given an integer. I have written this function to work with a switch statement: switch(i) { case 0: return hearts; ... } BUT, there must be a much cleaner way to do this, by parsing the enum type from the value - I just don't know the syntax and can't seem to find it. It must be something like: Rank r = Enum.Parse(Rank, i); ... of course that doens't work. But it must be something similar. Any suggestions?

      L Offline
      L Offline
      Libor Tinka
      wrote on last edited by
      #2

      Its easier than you would expect. Just convert the type: int i = 0; Suit suit = (Suit)i; // suit == Suit.Hearts int index = (int)suit; // index == 0;

      1 Reply Last reply
      0
      • B budidharma

        Alright, I have an enum called Suit ... public enum Suit { Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3, Null = -1, } And I have a function I need to create that will return the correct rank type given an integer. I have written this function to work with a switch statement: switch(i) { case 0: return hearts; ... } BUT, there must be a much cleaner way to do this, by parsing the enum type from the value - I just don't know the syntax and can't seem to find it. It must be something like: Rank r = Enum.Parse(Rank, i); ... of course that doens't work. But it must be something similar. Any suggestions?

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Simply casting to the enum type should work.

        Rank r = (Rank)i;

        Need to be careful though, this will compile and run even if the value of i is outside the range of Rank. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        B 1 Reply Last reply
        0
        • S S Senthil Kumar

          Simply casting to the enum type should work.

          Rank r = (Rank)i;

          Need to be careful though, this will compile and run even if the value of i is outside the range of Rank. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          B Offline
          B Offline
          budidharma
          wrote on last edited by
          #4

          Thanks! Works perfect! I followed your advice and added error checking to make sure the value is between 0 and NUMRANKS (and 0 AND NUMSUITS for suits). Thanks again!

          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