Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何仅在另一个方法已运行时执行该方法?_C#_Methods_Switch Statement - Fatal编程技术网

C# 如何仅在另一个方法已运行时执行该方法?

C# 如何仅在另一个方法已运行时执行该方法?,c#,methods,switch-statement,C#,Methods,Switch Statement,我有一个选择的用户在主要使用开关。根据用户的选择,稍后会有几个选择,程序将结束或不结束。至少,这是我正在努力实现的目标 //This is in Main string[] menyVal2 = new string[] {"Go to the hotel room", "Go to the dining hall"}; string title2 = "text"; int choice2 = menu(menuChoice2, title2);

我有一个选择的用户在主要使用开关。根据用户的选择,稍后会有几个选择,程序将结束或不结束。至少,这是我正在努力实现的目标

//This is in Main
string[] menyVal2 = new string[] {"Go to the hotel room", "Go to the dining hall"};
            string title2 = "text";
            int choice2 = menu(menuChoice2, title2);

            switch (choice2)
            {
                case 0:
                    hotelRoom();
                    break;
                case 1:
                    diningHall();
                    break;
            }
许多行代码之后

public static void save()
        {
            Console.Clear();
            string[] menuChoice = {"Chapter 2", "Back to start"};
            string title = "text";

            int choice = menu(menuChoice, title);
            switch (val)
            {
                case 0:
                    if (hotelRoom = true) //this if-statement does not work
                    {
                        withoutCross();
                    }
                    else if (diningHall = true)
                    {
                        withCross();
                    }
                    break;
                case 1:
                    Main(new string[] { });
                    break;
            }
        }

如果我正确理解了问题的标题,那么这就是一个解决方案:

将方法的返回类型设为bool,并将其填充到变量中

bool isMethodExecuted = MyMethod();
稍后,如果执行了该方法,则可以使用if语句进行检查:

if(isMethodExecuted)
    MyOtherMethod();

那么问题是什么呢?如果我正确理解了问题的标题,那么这就是一个解决方案:创建方法bool的返回类型并将其填充到变量。稍后您可以检查变量是否为true。然后执行另一个方法。那么您将哪个代码称为停止或不停止的开关?