954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get value of element in enum

I am making a simple blackjack game. I have now made the deck which i can draw cards from. But I don't know how to get the values from each card so that i can add them together.

I have a card class named Kort with the enum:

public enum KortVerdi { To, Tre, Fire, Fem, Seks, Sju, Åtte, Ni, Ti, Knekt, Dame, Konge, Ess };
public enum KortFarge { Hjerter, Kløver, Ruter, Spar };


And when i draw two cards with my draw method from my main i would like to get the values of the two cards so that i can add them together.

My main looks like this now:

static void Main(string[] args)
        {
            Kortstokk mineKort =new Kortstokk();

            string kort1 = Convert.ToString(mineKort.Trekk());
            string kort2 = Convert.ToString(mineKort.Trekk());

            Console.WriteLine("Du har fått dette: " + kort1 + " og " + kort2);
            Console.WriteLine(kort1 + " " + kort2);
            Console.ReadLine();
        }
Twistar
Newbie Poster
6 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

You can set the values of the enums so you never have a zero value.
You can give each card a value that you want.
You can have repeating values.
You can cast the enums as integers or other numeric types.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Thanks for the reply!
I have changed the values for the cards like this:

public enum KortVerdi { To=2, Tre=3, Fire=4, Fem=5, Seks=6, Sju=7, Åtte=8, Ni=9, Ti=10, Knekt=10, Dame=10, Konge=10, Ess=11 };


But when i draw the cards in my main i get output like "Two of Diamonds". How can I use this output to get the values from the enum?

Twistar
Newbie Poster
6 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Perhaps this will help you out:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enumtest
{
    class Program
    {
        public enum KortVerdi { To = 2, Tre = 3, Fire = 4, Fem = 5, Seks = 6, Sju = 7, Åtte = 8, Ni = 9, Ti = 10, Knekt = 10, Dame = 10, Konge = 10, Ess = 11 };
        static void Main(string[] args)
        {
             
            System.Array intAr = Enum.GetValues(typeof(KortVerdi));
            string str = intAr.GetValue(9).ToString(); //the variable str will contain "Knekt"
            
            int KortVal = (int)KortVerdi.Knekt;         // KortVal will contain the int value 10
           
            Console.ReadLine();
        }
    }
}


Success!

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

I like @ddanbe's example and it might work for your needs.

You could also use a dictionary depending on how much data you will have.
Since your "key" might have repeating values, I recommend an ILookup or a List of KeyValuePairs.
Here's an example:

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_409885_CS_CON
{
   using KVP_I2S = KeyValuePair<int, string>;
   using LST_KVP_I2S = List<KeyValuePair<int, string>>;
   class Program
   {
      static void Main(string[] args)
      {
         LST_KVP_I2S lst_kvp_i2sCards = new LST_KVP_I2S()
         {
            new KVP_I2S(2, "Two of Spades"),
            new KVP_I2S(2, "Two of Hearts"),
            new KVP_I2S(2, "Two of Clubs"),
            new KVP_I2S(2, "Two of Diamonds"),
            new KVP_I2S(3, "Three of Spades"),
            new KVP_I2S(3, "Three of Hearts"),
            new KVP_I2S(4, "Three of Clubs"),
            new KVP_I2S(4, "Three of Diamonds"),
            new KVP_I2S(11, "Ace of Spades"),
            new KVP_I2S(11, "Ace of Hearts"),
            new KVP_I2S(11, "Ace of Clubs"),
            new KVP_I2S(11, "Ace of Diamonds")
         };
         // Show the names of the cards that are less than an Ace
         foreach (KVP_I2S kvp in lst_kvp_i2sCards.Where(i => i.Key < 11))
         {
            Console.WriteLine(kvp.Value);
         }
      }
   }
}


If this is too complex, please let me know.

You could also use a class to hold each card and the properties of the class will hold the necessary strings and values.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Thanks for all the help! I figured it out now. I got the values i needed and added them together like this:

Kort.KortType kort1 = mineKort.Trekk();
Kort.KortType kort2 = mineKort.Trekk();

int verdi1 = Convert.ToInt32(kort1.Verdi);
int verdi2 = Convert.ToInt32(kort2.Verdi);
int sum = verdi1 + verdi2;
Twistar
Newbie Poster
6 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: