Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4076994
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:29:42+00:00 2026-05-20T17:29:42+00:00

I use class Card which contains 2 enumerated properties (suite – hearts diamonds spades

  • 0

I use class Card which contains 2 enumerated properties (suite – hearts diamonds spades and clubs) and card value from 2 to A. And overrides ToString() method to returns something like Ah Ad etc. All ok, but enum value can’t starts with number, therefore my card value enumerated looks like x2, x3, x4 … it is not beautiful.

Also need simple approach to parse few cards from single string.

Who know the best approach to design this class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:29:43+00:00Added an answer on May 20, 2026 at 5:29 pm

    Couldn’t you assign Jack, Queen, King, and Ace to be 11, 12, 13, and 14, respectively? It’d end up looking something like:

    public class Card
    {
        public int Value { get; private set; }
        public enum SuitType
        {
            Clubs, Spades, Hearts, Diamonds
        }
        public SuitType Suit { get; private set; }
        public Card(int value, SuitType suit)
        {
            Suit = suit;
            Value = value;
        }
        public Card(string input)
        {
            if (input == null || input.Length < 2 || input.Length > 2)
                throw new ArgumentException();
            switch (input[0])
            {
                case 'C': case 'c':
                    Suit = SuitType.Clubs;
                    break;
                case 'S': case 's':
                    Suit = SuitType.Spades;
                    break;
                case 'H': case 'h':
                    Suit = SuitType.Hearts;
                    break;
                case 'D': case 'd':
                    Suit = SuitType.Diamonds;
                    break;
                default:
                    throw new ArgumentException();
            }
            int uncheckedValue = (int)input[1];
            if (uncheckedValue > 14 || uncheckedValue < 1)
                throw new ArgumentException();
            Value = uncheckedValue;
        }
        public string encode()
        {
            string encodedCard = "";
            switch (Suit)
            {
                case SuitType.Clubs:
                    encodedCard += 'c';
                    break;
                case SuitType.Spades:
                    encodedCard += 's';
                    break;
                case SuitType.Hearts:
                    encodedCard += 'h';
                    break;
                case SuitType.Diamonds:
                    encodedCard += 'd';
                    break;
            }
            encodedCard += (char) Value;
            return encodedCard;
        }
        public override string ToString()
        {
            string output = "";
            if (Value > 10)
            {
                switch (Value)
                {
                    case 11:
                        output += "Jack";
                        break;
                    case 12:
                        output += "Queen";
                        break;
                    case 13:
                        output += "King";
                        break;
                    case 14:
                        output += "Ace";
                        break;
                }
            }
            else
            {
                output += Value;
            }
            output += " of " + System.Enum.GetName(typeof(SuitType), Suit);
            return output;
        }
    }
    

    Edit:
    I added some string functionality.
    I took structure of Card(string input) from Jon Hanna’s answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use class from How to support Arabic text in Android? to show Arabic
I have to use class Matrix4x4 from some 3rd party library, and I need
I have a card-game application which makes use of Single Table Inheritance. I have
I have a div with a class of 'card', which is also draggable() with
I have a text file that I am pulling from my SD card which
I use the following code to obtain Photos which are stored in a SD-card
I am trying to use Class::DBI with Catalyst::Plugin::Authentication::Store::DBIC. The example given on CPAN does
How to decide when to use object adapter and when to use class adapter?
To simulate enums in PHP I like to use class constants. e.g. class FRUIT
Use case: class MyTask(Task): queue = 'default_queue' def run(self): # do work Normally I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.