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. Enums and Ints

Enums and Ints

Scheduled Pinned Locked Moved C#
tutorialquestionlounge
6 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

    I'm confused on how to convert between enum types and integer types effeciently. Here's the code I'm working with: public enum Rank { Hearts = 0, Diamonds = 1, Spades = 3, Clubs = 4 } public enum Suit { Deuce = 0, Trey = 1, Four = 2, Five = 3, Six = 4, Seven = 5, Eight = 6, Nine = 7, Ten = 8, Jack = 9, Queen = 10, King = 11, Ace = 12 } Inside the card, the rank and suit are stored as: Rank rank; Suit suit; The point being: I have a function shuffle which shuffles the deck using for loops. public void Shuffle() { FillDeck(); Card tempCard = new Card(); Random rInt = new Random(); for (int i = 0; i < 100; i++) { int first = rInt.Next(51); int second = rInt.Next(51); // Copy deck[first] to tempCard // Copy deck[second] to deck[first] // Copy tempCard to deck[second] tempCard.Set(deck[first].Rank, deck[first].Suit); deck[first].Set(deck[second].Rank, deck[second].Suit); deck[second].Set(tempCard.Rank, tempCard.Suit); } } I'm attemping to pass integer values from the for loops into the Set function of card which takes Rank and Suit parameters. It's obviously causing errors. Can I simply typecast an integer to the equivalent suit or rank like so without causing problems, and is it proper programming practice or is there a better way to do it? tempCard.Set( (Rank)deck[first].Rank, (Suit)deck[first].Suit );

    W 1 Reply Last reply
    0
    • B budidharma

      I'm confused on how to convert between enum types and integer types effeciently. Here's the code I'm working with: public enum Rank { Hearts = 0, Diamonds = 1, Spades = 3, Clubs = 4 } public enum Suit { Deuce = 0, Trey = 1, Four = 2, Five = 3, Six = 4, Seven = 5, Eight = 6, Nine = 7, Ten = 8, Jack = 9, Queen = 10, King = 11, Ace = 12 } Inside the card, the rank and suit are stored as: Rank rank; Suit suit; The point being: I have a function shuffle which shuffles the deck using for loops. public void Shuffle() { FillDeck(); Card tempCard = new Card(); Random rInt = new Random(); for (int i = 0; i < 100; i++) { int first = rInt.Next(51); int second = rInt.Next(51); // Copy deck[first] to tempCard // Copy deck[second] to deck[first] // Copy tempCard to deck[second] tempCard.Set(deck[first].Rank, deck[first].Suit); deck[first].Set(deck[second].Rank, deck[second].Suit); deck[second].Set(tempCard.Rank, tempCard.Suit); } } I'm attemping to pass integer values from the for loops into the Set function of card which takes Rank and Suit parameters. It's obviously causing errors. Can I simply typecast an integer to the equivalent suit or rank like so without causing problems, and is it proper programming practice or is there a better way to do it? tempCard.Set( (Rank)deck[first].Rank, (Suit)deck[first].Suit );

      W Offline
      W Offline
      whizzs
      wrote on last edited by
      #2

      Whew, where to begin. First about the enums. I am assuming your Card class set method is structured as such: Card.Set ( Rank r, Suit s) and you have an array of Cards in deck[]. So to set your first card you would say deck[0].Set ( Rank.Hearts, Suit.Ace ). Now the first problem is you have Rank and Suit mixed up, the Suit should be hearts, clubs, etc. Confusing to read. Your second issue is your Card property Rank and Suit have the same name as your enum, that cannot be. I see what you are doing, you want to return that enum to set the next card with it, but you can't have the same name. I would change the name to CardRank and CardSuit and define the property like this: //member variables private Rank cardRank; private Suit cardSuit; //Properties public Rank CardRank { set { cardRank = value; } get { return cardRank; } } // do the same for CardSuit now your swap functions will be: tempCard.Set ( deck[first].CardRank, deck[first].CardSuit ); If you used integers in you Set function prototype then this won't work and you have to modify your properties to convert to integer or from integer depending on what your members are. Your third issue is your last card will never get shuffled, Random returns a number less than the number passed in and your first and second could theoretcially be the same card (not a real problem, just wasted a shuffle) If you want the integer value of an enum do this: int x = (int) Rank.Clubs; //now x = 4 or Rank r = Rank.Clubs; int x = (int) r; //x = 4 finally if you want the string value: string s = Rank.Clubs.ToString(); // s = "Clubs"

      B 1 Reply Last reply
      0
      • W whizzs

        Whew, where to begin. First about the enums. I am assuming your Card class set method is structured as such: Card.Set ( Rank r, Suit s) and you have an array of Cards in deck[]. So to set your first card you would say deck[0].Set ( Rank.Hearts, Suit.Ace ). Now the first problem is you have Rank and Suit mixed up, the Suit should be hearts, clubs, etc. Confusing to read. Your second issue is your Card property Rank and Suit have the same name as your enum, that cannot be. I see what you are doing, you want to return that enum to set the next card with it, but you can't have the same name. I would change the name to CardRank and CardSuit and define the property like this: //member variables private Rank cardRank; private Suit cardSuit; //Properties public Rank CardRank { set { cardRank = value; } get { return cardRank; } } // do the same for CardSuit now your swap functions will be: tempCard.Set ( deck[first].CardRank, deck[first].CardSuit ); If you used integers in you Set function prototype then this won't work and you have to modify your properties to convert to integer or from integer depending on what your members are. Your third issue is your last card will never get shuffled, Random returns a number less than the number passed in and your first and second could theoretcially be the same card (not a real problem, just wasted a shuffle) If you want the integer value of an enum do this: int x = (int) Rank.Clubs; //now x = 4 or Rank r = Rank.Clubs; int x = (int) r; //x = 4 finally if you want the string value: string s = Rank.Clubs.ToString(); // s = "Clubs"

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

        Awesome. Thanks for those pointers. That will certainly help me get sorted out. There is one issue, the big one, that you didn't mention however (I don't think). I have two integers from the four loops, s and r. s is between 0 and 3 and r is between 0 and 12. They correspond directly to the integer values I assigned the suits and ranks in the enum. From those integers, I need to store the suit and rank, in the for loops. So - I need to do something like: Rank rank = (Rank)r; // r is in integer. if r == 12, then this should set rank equal to Rank.Ace, however, you can't typecast an integer to the equavalent enum value. I don't need the string. You see what I'm saying?

        B 1 Reply Last reply
        0
        • B budidharma

          Awesome. Thanks for those pointers. That will certainly help me get sorted out. There is one issue, the big one, that you didn't mention however (I don't think). I have two integers from the four loops, s and r. s is between 0 and 3 and r is between 0 and 12. They correspond directly to the integer values I assigned the suits and ranks in the enum. From those integers, I need to store the suit and rank, in the for loops. So - I need to do something like: Rank rank = (Rank)r; // r is in integer. if r == 12, then this should set rank equal to Rank.Ace, however, you can't typecast an integer to the equavalent enum value. I don't need the string. You see what I'm saying?

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

          I just wrote my own static functions to do this, and added them to the card class: public static Rank ConvertToRank(int i); public static Suit ConvertToSuit(int i); ... just filled with switch statements to return the appropriote enum object. I was hoping there was a better way to do this though.

          M 1 Reply Last reply
          0
          • B budidharma

            I just wrote my own static functions to do this, and added them to the card class: public static Rank ConvertToRank(int i); public static Suit ConvertToSuit(int i); ... just filled with switch statements to return the appropriote enum object. I was hoping there was a better way to do this though.

            M Offline
            M Offline
            Marc 0
            wrote on last edited by
            #5

            In vb you can do

            Dim r as Integer '<- Random rank
            'Make r as random number
            CardRank = CType(r, Rank)

            I think in think in c# that would be

            int r;
            // Make r a random number
            CardRank = (Rank)r;

            But you say that doesn't work :confused: Pretty weird... Pompiedompiedom... ;)


            "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

            -- modified at 18:30 Thursday 27th October, 2005

            W 1 Reply Last reply
            0
            • M Marc 0

              In vb you can do

              Dim r as Integer '<- Random rank
              'Make r as random number
              CardRank = CType(r, Rank)

              I think in think in c# that would be

              int r;
              // Make r a random number
              CardRank = (Rank)r;

              But you say that doesn't work :confused: Pretty weird... Pompiedompiedom... ;)


              "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

              -- modified at 18:30 Thursday 27th October, 2005

              W Offline
              W Offline
              whizzs
              wrote on last edited by
              #6

              In C# use the Enum static method ToObject Suit myCard = (Suit) Enum.ToObject ( typeof ( Suit ), 12 ); This will set myCard to an ace (substitute the 12 for your integer you want the enum to be). Hope that helps

              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