johnnyboy johnnyboy - 1 year ago 109
C# Question

Saving/transferring Input Values Between Two Methods in c#

I'm a beginning programmer creating a console application based menu in c# and am trying to figure out how to store an integer from input from one method/menu and transfer it to another menu. Here's what I'm dealing with:

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):
Console.WriteLine("Invalid ID. Please enter a valid ID:");


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");


How would I best go about storing the user input from the SelectCustomer menu and then referencing it in the CustomerMenu? Thanks guys.

Answer Source

There are a few options, but I think the best would be to return the value from the first method and pass it as an argument to the second method:

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

public void CustomerMenu(int[] customerIDs) 
    // ... 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download