简体   繁体   中英

Menu not showing list results

I have this code that runs a menu but its not showing the list information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Teste_Menu
{
    class Program
    {
        static void Main(string[] args)
        {
            Menu();
            List<Modelo> ListaModelo = new List<Modelo>();
            ListaModelo.Add(new Modelo("Honda", "Civic", 180, 29000));
            ListaModelo.Add(new Modelo("Honda", "Jazz", 100, 15000));
            ListaModelo.Add(new Modelo("Honda", "HRV", 115, 22500));



        }

        static void Menu()
        {
            string escolha;

            do
            {
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Magenta;

                Console.WriteLine("\n");
                Console.WriteLine(" ==================================================================================================== ");
                Console.WriteLine(" =========================================== Carros ================================================= ");
                Console.WriteLine(" ==================================================================================================== \n\n");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(" * Consultar Lista de Carros -----------------------------------------------> (1)\n  ");


                escolha = Console.ReadLine();

                switch (escolha)
                {
                    case "1": ListaModelo();
                    break;
                }
                Console.ReadLine();

        }
        while (escolha != "2");
        }

        static void ListaModelo()
        {
             {
                 var ListaModelo = new List<int>(Enumerable.Range(0, 3));

                 ListaModelo.ForEach(Console.WriteLine);
             }
        }

    }
}

The problem I have is that when I press 1 the menu is not showing the car list. It just prints:

1
2
3
4

And no names. What am I doing wrong here?

You create in your method ListaModelo a completely new list named ListaModelo which is filled only with numbers. Although it has the same name, it is not the same List as the one which you instantiated in you Main method in this line:

List<Modelo> ListaModelo = new List<Modelo>();

One of the things you could do to solve your problem is to put the declaration of the List outside of the Main method and make it static :

class Program
{

    public static List<Modelo> ListaModelo = new List<Modelo>();

    static void Main(string[] args)
    {

and remove the line in your ListaModelo() method :

var ListaModelo = new List<int>(Enumerable.Range(0, 3));

To have a meaningful output you need to override the ToString method in the Modelo class. So it will display all the properties that you would like to see. Insert this method into your Modelo class:

public override string ToString()
{
    return String.Format("{0} {1} {2} {3}", yourProperty1, yourProperty2, yourProperty3, yourProperty4);
}

Also please make sure that the name of the method to print and the name of the List ListaModelo are different.

One last point! you need to fill your list with cars before showing the Menu otherwise it will be empty! Just switch the order to:

List<Modelo> ListaModelo = new List<Modelo>();
ListaModelo.Add(new Modelo("Honda", "Civic", 180, 29000));
ListaModelo.Add(new Modelo("Honda", "Jazz", 100, 15000));
ListaModelo.Add(new Modelo("Honda", "HRV", 115, 22500));

Menu();
  1. If you want to write the car list, you should have have a argument in Menu function:

    static void Menu(List param)

    1. In ListaModelo same problem, you should have List as argument:

    2. Then you can have ListaModelo function like this:

      static void ListaModelo(List param) {

        param.ForEach(x=> Console.WriteLine(x.Name)); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM