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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:14:11+00:00 2026-06-03T05:14:11+00:00

I am making a text adventure in C# , and someone suggested that I

  • 0

I am making a text adventure in C#, and someone suggested that I use a dispatch table instead of a switch statement.

Here’s the switch statement code:

        #region Public Methods
        public static void Do(string aString)
        {
                if(aString == "")
                        return;

                string verb = "";
                string noun = "";

                if (aString.IndexOf(" ") > 0)
                {
                        string[] temp = aString.Split(new char[] {' '}, 2);
                        verb = temp[0].ToLower();
                        noun = temp[1].ToLower();
                }
                else
                {
                        verb = aString.ToLower();
                }

                switch(Program.GameState)
                {
                        case Program.GameStates.Playing:
                                if (IsValidInput(Commands, verb, true))
                                {
                                        switch(verb) //this is the switch statement
                                        {
                                                case "help":
                                                case "?":
                                                        WriteCommands();
                                                        break;
                                                case "exit":
                                                case "quit":
                                                        Program.GameState = Program.GameStates.Quit;
                                                        break;
                                                case "move":
                                                case "go":
                                                        MoveTo(noun);
                                                        break;
                                                case "examine":
                                                        Examine(noun);
                                                        break;
                                                case "take":
                                                case "pickup":
                                                        Pickup(noun);
                                                        break;
                                                case "drop":
                                                case "place":
                                                        Place(noun);
                                                        break;
                                                case "use":
                                                        Use(noun);
                                                        break;
                                                case "items":
                                                case "inventory":
                                                case "inv":
                                                        DisplayInventory();
                                                        break;
                                                case "attack":
                                                        //attack command
                                                        break;
                                        }
                                }
                                break;

                        case Program.GameStates.Battle:
                                if(IsValidInput(BattleCommands, verb, true))
                                {
                                        switch(verb) //this is the other switch statement
                                        {
                                                case "attack":
                                                        //attack command
                                                        break;
                                                case "flee":
                                                case "escape":
                                                        //flee command
                                                        break;
                                                case "use":
                                                        //use command
                                                        break;
                                                case "items":
                                                case "inventory":
                                                case "inv":
                                                        //items command
                                                        break;
                                        }
                                }
                                break;
                }
        }
        #endregion

How do I refactor this to use a dispatch table?

  • 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-03T05:14:12+00:00Added an answer on June 3, 2026 at 5:14 am

    Simplest way would be to use a dictionary of delegates.

    For example:

    Dictionary<string, Action> dispatch = new Dictionary<string, Action>();
    
    dispatch["help"] = new Action(() => Console.WriteLine("Hello"));
    dispatch["dosomething"] = new Action(() =>
    {
        // Do something else
        Console.WriteLine("Do Something");
    });
    
    // Call the 'help' command
    dispatch["help"]();
    

    For multiple different parameters it might be simplest to use a base Delegate and use dynamic Invoke.

    Dictionary<string, Delegate> dispatch = new Dictionary<string, Delegate>();
    
    dispatch["help"] = new Action(() => Console.WriteLine("Hello"));
    dispatch["dosomething"] = new Action<string>(s => Console.WriteLine(s));
    
    dispatch["help"].DynamicInvoke();
    dispatch["dosomething"].DynamicInvoke("World");
    

    And if using .NET 4 you can also use dynamic types to resolve at run time to reduce the clutter slightly of dynamic invoke.

    Dictionary<string, dynamic> dispatch = new Dictionary<string, dynamic>();
    
    dispatch["help"] = new Action(() => Console.WriteLine("Hello"));
    dispatch["dosomething"] = new Action<string>(s => Console.WriteLine(s));
    
    dispatch["help"]();
    dispatch["dosomething"]("World");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started making a text-adventure in javascript this weekend. I realized that the
I am looking into making a text based game that I wrote in Haskell
please can anyone help me in making the text area in the following code
I have a text file that looks like: 6789 Here is a bunch of
I'm making a text based adventure game. I want to have the game name
I'm making a python text adventure. It is based off of ex41 in Learn
I'm making a text-based game, and I just finished the majority of code, and
I am making a text adventure engine in Java, and in order to save
Alright so Im making a text adventure game in C++ and I am quite
For an online course in python, I'm making a basic text-based adventure game in

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.