在c#中保存/传输两个方法之间的输入值

时间:2016-09-30 18:30:33

标签: c# menu console-application

我是一名初学程序员,在c#中创建一个基于控制台应用程序的菜单,并试图找出如何从一个方法/菜单的输入中存储一个整数并将其传输到另一个菜单。这是我正在处理的事情:

public void SelectCustomer()
{

    //Prompts the user for the customer’s ID
    //If the customer does not exist then display an error
    //If the customer does exist then display the Customer menu

    int input;
    Console.WriteLine("Please enter your customer ID:");
    input = Convert.ToInt32(Console.ReadLine());
    switch (input)
    {
        case (1 - 5):
            CustomerMenu();
            break;
        default:
            Console.WriteLine("Invalid ID. Please enter a valid ID:");
            break;
    }
    int[] CustomeridArray = { input, input, input, input, input };
}

public void CustomerMenu()
{
    //Display customer name, ID, and current order price
    //Display a menu with the following options:

    //1)Display current order
    //2)Add a product to the order
    //3)Remove a product from the order
    //4)Finalize the order
    //5)Return to Manager Customers menu

    int[] CustomerIDarray = new int[5] { 1, 2, 3, 4, 5 };

    Console.WriteLine("1) Display current order");
    Console.WriteLine("2) Add product to order");
    Console.WriteLine("3) Remove product from order");
    Console.WriteLine("4) Finalize order");
    Console.WriteLine("5) Return to Manage Customers Menu");
    Console.ReadLine();
    Console.ReadKey();
}

我如何才能最好地存储SelectCustomer菜单中的用户输入,然后在CustomerMenu中引用它?多谢你们。

1 个答案:

答案 0 :(得分:0)

有几个选项,但我认为最好的方法是从第一个方法返回值并将其作为参数传递给第二个方法:

public int32[] SelectCustomer() 
{ 
    // all your code
    return CustomeridArray;
}

public void CustomerMenu(int[] customerIDs) 
{ 
    // ... 
}