将方法移动到其他类时的堆栈溢出异常 [英] Stack overflow exception when moving methods to other classes

查看:63
本文介绍了将方法移动到其他类时的堆栈溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在使用控制台应用程序中的C#游戏。我试图将您可以使用的命令移动到单独的类文件中,现在它们产生了StackOverflow错误。粗。



我有4个文件:Program.cs(处理游戏循环和方法输入和处理),BasicCommands(包含5个命令方法),SpeechCommands(与基本相同)除了专门用于语音字符串)和Character(用于玩家角色和NPC的角色构造函数/对象)。



Hello,

I'm working on a C# game in a console application. I tried to move my "commands" that you can use into separate class files, and now they produce a StackOverflow error. Rough.

I have 4 Files: Program.cs (Handles the Game loop and method input and processing), BasicCommands(Holds 5 Methods for commands), SpeechCommands(Same as Basic except specifically for speech strings) and Character (character constructor/object for the player character and NPCs).

namespace DigitalLove
{
    public class Program
    {
        public List<string> validWords = new List<string> { "look",
            "move",
            "check status",
            "hallway",
            "dance room",
            "bathroom",
            "punch bowl",
            "talk",
            "the scrawny boy",
            "the tall boy",
            "the girl",
            "take",
            "cup of punch",
            "the blond boy",
            "Rock",
            "Blues",
            "Forte",
            "Roll"
            };
        public List<string> TalkingResponse = new List<string>
        {
            "uh...hi.",
            "of course not.",
            "yes",
            "no",
            "just here to have a good time.",
            "looking for people",

        };
        public string[] Locations = { "Dance Room", "Bathroom", "Hallway", "Punch Bowl" };
        public string Location,
        response,
        PlayerName,
        HeldItem;
        public Character Rockman;
        public Character Blues;
        public Character Forte;
        public Character Roll;
        public Character Player;
        public bool HoldingItem;
        SpeechCommands Command = new SpeechCommands();
        BasicCommands Basic = new BasicCommands();
        static void Main(string[] args)
        {
            Program Game = new Program();
            Game.Game();
        }
        public void Game()
        {
            //Game title here
            Console.WriteLine("Press enter to begin!");
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("What is your name?");
            PlayerName = Console.ReadLine();
            while (PlayerName == "")
            {
                Console.WriteLine("Please enter a name!");
                PlayerName = Console.ReadLine();
            }
            Console.WriteLine("Thank you, " + PlayerName + ". Good luck!");
            Console.WriteLine("Press enter to continue. . .");
            Console.ReadKey();
            Console.Clear();
            Location = Locations[0];
            Rockman = new Character();
            Blues = new Character();
            Forte = new Character();
            Roll = new Character();
            Player = new Character();

            //Player definitons
            Player.CharacterName = PlayerName;
            Player.Location = Location;

            //Rockman definitons
            Rockman.CharacterName = "Rock";
            Rockman.Location = Locations[0];
            Rockman.MetTrigger = false;

            //Blues definitions
            Blues.CharacterName = "Blues";
            Blues.Location = Locations[0];
            Blues.MetTrigger = false;

            //Forte definitions
            Forte.CharacterName = "Forte";
            Forte.Location = Locations[1];
            Forte.MetTrigger = false;

            //Roll definitons
            Roll.CharacterName = "Roll";
            Roll.Location = Locations[3];
            Roll.MetTrigger = false;
            Console.WriteLine("You stand near the edge of the dance, shying away from everyone else who seems to be having a very good time." + "\n" +
                "You would love to join, but no one has approached you yet! You feel awkward trying to approach anyone, despite the numerous amount of people in the same position.");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Command List: Look, Move, Check Status");
            Console.ResetColor();
            GetInput();
        }
        public string GetInput()
        {
            var Test = true;

            while (Test)
            {
                response = Console.ReadLine().ToLower();
                if (validWords.Contains(response))
                {
                    Test = false;
                    ProcessInput(response);
                }
                else
                {
                    Console.WriteLine("I'm sorry, I do not understand.");
                }
            }
            return response;
        }

        public void ProcessInput(string response)
        {
            switch (response)
            {
                case "look":
                Basic.LookCommand();
                break;
                case "move":
                Basic.MoveCommand();
                break;
                case "Move":
                Basic.MoveCommand();
                break;
                case "check status":
                Basic.CheckStatus();
                break;
                case "hallway":
                Basic.MoveCommand();
                break;
                case "dance room":
                Basic.MoveCommand();
                break;
                case "bathroom":
                Basic.MoveCommand();
                break;
                case "punch bowl":
                Basic.MoveCommand();
                break;
                case "talk":
                Basic.TalkCommand();
                break;
                case "the scrawny boy":
                Basic.TalkCommand();
                break;
                case "the tall boy":
                Basic.TalkCommand();
                break;
                case "the girl":
                Basic.TalkCommand();
                break;
                case "take":
                Basic.TakeCommand();
                break;
                case "cup of punch":
                Basic.TakeCommand();
                break;
                case "the blond boy":
                Basic.TalkCommand();
                break;
            }
        }
        public string GetTalkInput()
        {
            var Test = true;
            while (Test)
            {
                response = Console.ReadLine().ToLower();
                if (TalkingResponse.Contains(response))
                {
                    Test = false;
                    ProcessTalkInput(response);
                }
                else
                {
                    Console.WriteLine("I'm sorry, I do not understand.");
                }
            }
            return response;
        }
        public void ProcessTalkInput(string response)
        {
            switch (response)
            {
                case "uh...hi.":
                Basic.TalkCommand();
                break;
                case "of course not.":
                Basic.TalkCommand();
                break;
                case "yes":
                Basic.TalkCommand();
                break;
                case "no":
                Basic.TalkCommand();
                break;
            }
        }

    }
}



发生异常的地方在哪里?为什么会这样?我不太了解堆栈溢出异常。 Visual Studio中的调用堆栈只显示外部代码。



我尝试过:



我已经尝试减少我在其他响应中调用方法的次数。


Where is the exception happening? Why is it happening? I don't know much about the stack overflow exception. The call stack in Visual Studio only says "external code".

What I have tried:

I've tried cutting back on how many times I call the methods in the other responses.

推荐答案

Quote:

发生异常的地方在哪里?为什么会这样?

Where is the exception happening? Why is it happening?



使用调试器找到错误的位置,你可能会发现这个例程无休止地调用它。



当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么代码正在做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里显示你的代码是什么正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。


Use the debugger to locate where you get the error, you may discover that routine is calling itself endlessly.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


我发现发生了什么 - 我的类文件被无限期初始化。谢谢你的帮助!
I found out what was happening- my class files were being initialized indefinitely. Thanks for your help!


这篇关于将方法移动到其他类时的堆栈溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆