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 7585409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:10:10+00:00 2026-05-30T19:10:10+00:00

For a personal project, I’m working on a small web-based game. I have a

  • 0

For a personal project, I’m working on a small web-based game.

I have a Card class that has a Status property, and there are case statements all over the place. I thought, hey, this is a great oppurtunity for Replace Conditional with Polymorphism!

The problem is, I have a couple methods that do stuff like this:

public class Card
{
    public void ChangeStatus()
    {
      switch (Status)
      {
        case MyStatusEnum.Normal:
          Status = MyStatusEnum.Underwater;
          break;
        case MyStatusEnum.Underwater:
          Status = MyStatusEnum.Dead;
          break;
        // etc...
      }
    }
}

When refactoring it in the new NormalCard class, I’m overriding the ChangeStatus method like this:

public override void ChangeStatus()
{
    base.Status = MyStatusEnum.Underwater;
}

The problem is this object of NormalCard has a status of Underwater. I can’t reassign the type of this, and I don’t really want to change the return of the methods from void to CardBase. What options do I have? Is there a standard way of doing this?

Edit Tormod set me straight. I want the State Pattern. Thanks all!

  • 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-30T19:10:11+00:00Added an answer on May 30, 2026 at 7:10 pm

    In your case, I’d have a Card object that contains a CardStatus property. The subtypes of CardStatus correspond to the previous enum values. Refactor behaviour that depends on the current status except the state transition to be inside the CardStatus subtypes.

    The state transition that’s in your first example should, IMO, remain inside the Card object. The state changing feels more like a behaviour of the containing card than of the state object. What you can do is have the CardStatus objects tell you what state to transition to after an event.

    A rough example: (obviously there’s many more variations on this that could be used.)

    API

    interface ICardStatus {
        ICardStatus NextStatus(Card card);
    
        void DoStuff(Card card);
    }
    
    class Card {
        ICardStatus Status = new NormalCardStatus();
    
        void DoStuff() {
            Status.DoStuff(this);
        }
    
        void ChangeStatus() {
            Status = Status.NextStatus(this);
        }
    }
    

    Status implementations

    class NormalCardStatus : ICardStatus {
        ICardStatus NextStatus(Card card) {
            return new UnderwaterCardStatus();
        }
    
        void DoStuff(Card card) {
            // ...
        }
    }
    
    class UnderwaterCardStatus : ICardStatus {
        ICardStatus NextStatus(Card card) {
            return new DeathStatus();
        }
    
        void DoStuff(Card card) {
            // ...
        }
    }
    
    class DeathCardStatus : ICardStatus {
        ICardStatus NextStatus(Card card) {
            // ...
        }
    
        void DoStuff(Card card) {
            throw new Exception("Cannot do anything while dead");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a personal project I'm working on that requires Microsoft SAPI5 -- text
I have a personal project that's been online for sometime now. I've been keeping
I have a personal project I'm planning and I came to a small hurdle.
I'm working on a personal project (C# / ASP.NET) that will use LINQ to
I am working on a personal project based in PHP and MySQL, and I
I have an old personal project written in Java 1.4 that I am porting
I have a personal project that I'd like to build as an add-in for
I'm working a personal project that's going to include a home-screen widget updated with
I'm working on a personal project (Search engine) and have a bit of a
I'm working on a personal project involving microphones in my apartment that I can

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.