Home > Mobile >  How can I save user input in another method and still keep it on return to the method again
How can I save user input in another method and still keep it on return to the method again

Time:01-30

Newbie here learning c#. I have a question regarding saving user input. I have two methods, main and the car method. In the car method i have a list with three cars already in it. While in the car method the user can choose to add a new car to the list or print out all cars in that list. When adding a new car and after choosing to print out the list, the program shows all the cars including the newly added one. But on returning to the menu in main method and then entering the car method again, the list is reset to only hold the three cars that is put in the list from start. Is there a way to actually save the input from user, store it in a list or something else and then be able to access it to print it out? As mentioned, I'm a total beginner here so i hope I'm making sense and really would appriciate all feedback and tips! Thank you.

using System;
using System.Collections.Generic;
    

class Program{

  public static void Main(string [] args){

    int menuSelect = 0;
    
    do{

      // Printing menu options and taking user input
      Console.WriteLine("1. Cars "   "\n2. Exit");
      Console.WriteLine("Enter a option: ");
      menuSelect = Convert.ToInt32(Console.ReadLine());

      // Go to Cars method
      if(menuSelect == 1){

        Cars();

      // Message to user if input is higher than menu allows
      } else if (menuSelect > 2){
        Console.WriteLine("\nInvalid Choise");
      }


    }while(menuSelect != 2);

    Console.WriteLine("\nPress 'Enter' to exit...");
    Console.ReadLine();

  }

  public static void Cars(){

    // List containing cars. Resets every time entering the method to
    // just BMW, Volvo and Fiat. Regardless if the user has added a new car.
    List <string> carList = new List <string> {"BMW", "Volvo", "Fiat"};
    int carMenuSelect = 0;

    do{

        // Printing menu and taking user input
        Console.WriteLine("1. Add Car"   "\n2. Show Cars"   "\n3. Exit");
        Console.WriteLine("Enter a option: ");
        carMenuSelect = Convert.ToInt32(Console.ReadLine());

    // Lets the user add a new car to the list
    if (carMenuSelect == 1){

      Console.WriteLine("Enter new car: ");
      string newCar = Console.ReadLine();

      Console.WriteLine("New Car Added!");
      carList.Add(newCar);

    } else if(carMenuSelect == 2){

      foreach (string car in carList){

        Console.WriteLine(car);

      }

    } else if(carMenuSelect > 3){

      Console.WriteLine("\nInvalid option...");

    }

    }while(carMenuSelect != 3);

    Console.WriteLine("Press 'Enter' to return to menu....");
    Console.ReadLine();

}
  
}

CodePudding user response:

the func Car creates a new list each time it is called, so you should create a list in the main func and send it to the function by ref to add items to it. Code:

public static void Main(string [] args){
List <string> carList = new List <string> {"BMW", "Volvo", "Fiat"};
Car(carList);
}

public static void Cars(ref List<string> myList){
myList.Add("new Car");
}

or Your cand send the list then return the modified list and save it in the original one. Code:

public static void Main(string [] args){
List <string> carList = new List <string> {"BMW", "Volvo", "Fiat"};
carList = Car(carList);
}

public static List<string> Cars(List<string> myList){
myList.Add("new Car");
return myList;
}
  •  Tags:  
  • Related