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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:44:54+00:00 2026-06-04T19:44:54+00:00

First i’ll just state that I’m using singletons that won’t actually be around throughout

  • 0

First i’ll just state that I’m using singletons that won’t actually be around throughout the entire app life time. It’s more a way of encapsulating what is going on from the user.

I have several GameStates, eg InGame or MainMenu, and these have some quite similar function calls so I thought to use inheritence to stop copy / pasting. The following code is what I have, but it does not work as I intend. Here it is:

BaseState.cs

abstract class BaseState
{
    protected static BaseState mHandle = null;

    protected static BaseState Handle
    {
        get
        {
            return mHandle;
        }
        set
        {
            mHandle = value;
        }
    }

    public static GameState UpdateState(GameTime gameTime)
    {
        GameState g = GameState.MainMenu;

        try
        {
            Handle.Update(gameTime);
        }
        catch (Exception e)
        {

        }

        return g;
    }

    public static void DrawState(GameTime gameTime, SpriteBatch spriteBatch)
    {
        Handle.Draw(gameTime, spriteBatch);
    }

    public static void Release()
    {
        mHandle = null;
    }

    protected abstract GameState Update(GameTime gameTime);
    protected abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch);
}

InGame.cs

class InGame : BaseState
{
    private InGame()
    {

    }

    protected static new BaseState Handle
    {
        get
        {
            if (mHandle == null)
            {
                mHandle = new InGame();
            }

            return mHandle;
        }
        set
        {
            mHandle = value;
        }
    }

    protected override GameState Update(GameTime gameTime)
    {
        return GameState.Quit;
    }

    protected override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {

    }
}

You can probably tell that I want to be able to use the get and set of InGame in the BaseState so I can simply call Handle.Update() and no matter if I called it from InGame or Menu or whatever it would know which code to use.

Obviously I need to brush up on my OO skills. But if anyone could suggest a way to get this to do what I would like it to, or to suggest a different way of going about it I would be grateful. Thanks.

  • 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-06-04T19:44:56+00:00Added an answer on June 4, 2026 at 7:44 pm

    What you are trying to achieve does not require a singleton, see below:

    public abstract class BaseState
    {
        public GameState UpdateState(GameTime gameTime)
        {
            GameState g = GameState.MainMenu;
    
            try
            {
                g = Update(gameTime); // Update returns a new state
            }
            catch (Exception e)
            {
    
            }
    
            return g;
        }
    
        protected abstract GameState Update(GameTime gameTime);
        protected abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch);
    }
    
    public class InGame : BaseState
    {
        public InGame()
        {
    
        }
    
        protected override GameState Update(GameTime gameTime)
        {
            return GameState.Quit;
        }
    
        protected override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // Draw game
        }
    }
    
    public class InMenu : BaseState
    {
        public InMenu()
        {
    
        }
    
        protected override GameState Update(GameTime gameTime)
        {
            return GameState.Pause;
        }
    
        protected override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // Draw menu
        }
    }
    
    public void Foo()
    {
        List<BaseState> states = new List<BaseState>();
        states.Add(new InGame());
        states.Add(new InMenu());
    
        // Calls InGame.Update(...)
        states[0].UpdateState(...);
    
        // Calls InMenu.Update(...)
        states[1].UpdateState(...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I have an app that provides the user information about the amount
First, I have a list of QWidget s that I won't know the length
We're building an app, our first using Rails 3, and we're having to build
First of all, I just installed XE2 for the first time, and plan to
First off, I'm working on an app that's written such that some of your
First time I am using the persist() method in a project. With JPA I
First, I am using Live connect, as documents says live id will be obsolete
first I used onDestroy() method as below to make something when my app is
First time posting here, will try to be succinct. This is a classic 'can't
First, I am a SQL noob, second if there is a better solution that

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.