You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

编程新手求助:如何在Main函数中调用含switch的自定义类方法

Solution to Call Priceofservices.SuperSwitch from Main

Hey Tomasz, let's get this sorted out so selecting option 3 in your main menu launches the service pricing switch interface. Here's what you need to adjust:

Step 1: Make the SuperSwitch Method Accessible

Right now, your SuperSwitch method is private (the default access modifier for class members in C#), which means your Program class can't call it. We need to change its access level to public or internal (since both classes are in the same ComputerServiceTSz namespace, internal works too).

Updated Priceofservices class code:

using System;
namespace ComputerServiceTSz
{
    class Priceofservices
    {
        // Change to public/internal so other classes can call it
        public void SuperSwitch()
        {
            int x = 0;
            Console.WriteLine("Please, choose type of service.");
            Console.WriteLine("1 - Cleaning");
            Console.WriteLine("2 - Repair");
            Console.WriteLine("3 - Upgrade");
            x = int.Parse(Console.ReadLine());
            switch (x)
            {
                case 1:
                    Console.WriteLine("Cleaning service selected!"); // Add your pricing logic here
                    break;
                case 2:
                    Console.WriteLine("Repair service selected!"); // Add your pricing logic here
                    break;
                case 3:
                    Console.WriteLine("Upgrade service selected!"); // Add your pricing logic here
                    break;
                default:
                    Console.WriteLine("Invalid option, please try again.");
                    break;
            }
            Console.ReadKey(); // Let user read the result before returning to main menu
        }
    }
}

Step 2: Update the Main Menu's Case 3

Your current code merges case 3 and 4, which means selecting 3 quits the program—we need to split them. In case 3, we'll create an instance of Priceofservices and call its SuperSwitch method.

Updated Program class code:

using System;
using System.Collections.Generic;
namespace ComputerServiceTSz
{
    class Program
    {
        static void Main(string[] args)
        {
            bool done = false;
            while(!done)
            {
                int x = 0;
                Console.WriteLine("Welcome to computer repair automatic helpdesk.");
                Console.WriteLine("1 - List of currently avaivable servisants");
                Console.WriteLine("2 - List of services");
                Console.WriteLine("3 - Price of services");
                Console.WriteLine("4 - Quit");
                Console.WriteLine("<------------------------------------------------------>");
                x = int.Parse(Console.ReadLine());
                switch (x)
                {
                    case 1:
                        {
                            List<Servicemanlist> lista = Servicemanlist.CreateServicemanlist();
                            foreach (var item in lista)
                            {
                                Console.WriteLine(item);
                            }
                            Console.ReadKey();
                        }
                        break;
                    case 2:
                        {
                            List<string> lista = Serviceslist.CreateServicelist();
                            foreach (var item in lista)
                            {
                                Console.WriteLine(item);
                            }
                            Console.ReadKey();
                        }
                        break;
                    case 3:
                        {
                            // Create an instance of Priceofservices and call SuperSwitch
                            Priceofservices servicePricing = new Priceofservices();
                            servicePricing.SuperSwitch();
                        }
                        break;
                    case 4:
                        {
                            Console.WriteLine("Bye, see you next time!");
                            done = true;
                            Console.ReadKey();
                        }
                        break;
                    default:
                        Console.WriteLine("Invalid option, please enter a number between 1-4.");
                        Console.ReadKey();
                        break;
                }
            }
        }
    }
}

Key Notes:

  • Instance vs Static: Since SuperSwitch is an instance method, we need to create an object of Priceofservices (with new Priceofservices()) before calling the method. If you wanted to skip instantiation, you could make it static with public static void SuperSwitch() and call it directly with Priceofservices.SuperSwitch().
  • Input Validation: I added a default case in both switches to handle invalid user input—this is a good practice to prevent crashes from bad inputs.
  • User Experience: The Console.ReadKey() in SuperSwitch lets users read the selected service result before returning to the main menu, making the flow smoother.

内容的提问来源于stack exchange,提问作者Tomasz Szabała

火山引擎 最新活动