简体   繁体   English

C#列表实例为空

[英]c# List instance is empty

My problem is that I have a List in one class "Menu" containing objects of the class "Pizza". 我的问题是我在“菜单”类中有一个包含“比萨饼”类对象的列表。 I want to access this list in a third class "Order". 我想在第三类“订单”中访问此列表。 I am doing it in console. 我正在控制台中进行。

class Menu
{
    public List<Pizza> pizzaOnMenu = new List<Pizza>();

    public void CreateElementToMenu()
    { 
        pizzaOnMenu.Add(new Pizza(1, "Magarita", "Tomato, cheese", 49));
        pizzaOnMenu.Add(new Pizza(2, "Hawaii", "Tomato, cheese, ham, pineapple", 55));
        pizzaOnMenu.Add(new Pizza(3, "Cappriciossa", "Tomato, cheese, ham, mushroom", 55));
        pizzaOnMenu.Add(new Pizza(4, "Special", "Tomato, cheese, beef", 60));
    }
}

I have also tried to make this as well 我也试图做到这一点

public List<Pizza> pizzaOnMenu(){ get; set; }

Anyway - the method is called from Main(): 无论如何-该方法是从Main()调用的:

Menu menu = new Menu();
menu.CreateElementToMenu();

And then I try to access the list in "Order" to check if the pizza exist on the list in "Menu": 然后,我尝试访问“订单”中的列表,以检查比萨饼是否存在于“菜单”中的列表中:

class Order
{

    private List<Pizza> orderList = new List<Pizza>();

    public void CreateOrder()
    {
        Console.WriteLine("\nWhich number from the menu do you wish to order?");
        int orderNo = int.Parse(Console.ReadLine());

        Menu menu = new Menu();
        Pizza wantedPizza = null;

        foreach (Pizza p in menu.pizzaOnMenu)
        {
            if (p.Nr == orderNo)
            {
                wantedPizza = p;
                break;
            }
         }

        if (wantedPizza != null)
        {
            orderList.Add(wantedPizza);
            Console.WriteLine("\n" + wantedPizza.PizzaName + " is added to your order");
        }    
        else
        {
            Console.WriteLine("\nThe pizza with no. " + orderNo + " was not found!");
        }
    }

However, when the method CreateOrder() is run from main and I eg choose no. 但是,当方法CreateOrder()从main运行时,例如选择no。 2 from the menu, it writes "...was not found" instead of "...is added to your order" - it seems that the list (from "Order") is empty. 2从菜单中,它写上“ ...未找到”,而不是“ ...已添加到您的订单中”-列表(来自“订单”)似乎为空。 But I am able to print out the list created from Menu :S (if that makes sense?) 但是我可以打印出从Menu:S创建的列表(如果有意义)?

Thanks in advance. 提前致谢。

Look at the foreach loop over the contents of menukort.pizzaOnMenu : I don't see a closing brace ( } ) where the indentation would indicate it is expected (immediately after the closing brace of the if. 查看menukort.pizzaOnMenu的内容上的foreach循环:我没有看到缩进括号( } ),其中缩进将表明期望缩进(紧接menukort.pizzaOnMenu括号之后)。

Thus the if (wantedPizza != null) is checked for every possible pizza. 因此,为每个可能的披萨检查if (wantedPizza != null)

If this is just a cut and paste error when creating the question: edit the question having checked the code you copied in is a true copy, and then make the correction clear. 如果这只是在创建问题时出现剪切和粘贴错误:请在检查复制的代码是否为真实副本后编辑问题,然后使更正明确。

You probably want to move calling the initialization to constructor of the Menu class, providing that Menukort and Menu are same classes: 您可能希望将调用初始化转移到Menu类的构造函数,前提是Menukort和Menu是相同的类:

class Menu
{
    public Menu()
    {
        CreateElementToMenu();
    }

    public List<Pizza> pizzaOnMenu = new List<Pizza>();

    public void CreateElementToMenu()
    { 
        pizzaOnMenu.Add(new Pizza(1, "Magarita", "Tomato, cheese", 49));
        pizzaOnMenu.Add(new Pizza(2, "Hawaii", "Tomato, cheese, ham, pineapple", 55));
        pizzaOnMenu.Add(new Pizza(3, "Cappriciossa", "Tomato, cheese, ham, mushroom", 55));
        pizzaOnMenu.Add(new Pizza(4, "Special", "Tomato, cheese, beef", 60));
    }
}

Your order class use Menukort object instead of Menu what i don't understand why 您的订单类使用Menukort对象而不是Menu我不明白为什么

You have public void CreateElementToMenu() in Menu class. 您在Menu类中具有public void CreateElementToMenu()

Perhaps you should use this code in your Order class 也许您应该在Order类中使用此代码

Menu menu = new Menu();
menu.CreateElementToMenu();

使用public static class public class

Just another approach to make it a little more SOLID - see http://en.wikipedia.org/wiki/SOLID_(object-oriented_design : 使其更加坚固的另一种方法-参见http://en.wikipedia.org/wiki/SOLID_(object-oriented_design

public class OrderDataEntry
{
    public static void TakeOrder()
    {
        Console.Write("\nWhich number from the menu do you wish to order?");
        int orderNo = int.Parse(Console.ReadLine());

        Menu menukort = new Menu();
        Pizza wantedPizza = menukort.pizzaOnMenu.FirstOrDefault<Pizza>(p => p.OrderNumber.Equals(orderNo));

        if (wantedPizza != null)
        {
            Console.WriteLine("\n" + wantedPizza.PizzaName + " is added to your order");
            Order o = new Order(wantedPizza);
        }
        else
        {
            Console.WriteLine("\nThe pizza with no. " + orderNo + " was not found!");
        }
    }
}

public class Order
{
    private List<Pizza> orderList; 

    public Order(Pizza wantedPizza)
    {
        orderList = new List<Pizza>();
        AddToOrder(wantedPizza);
    }

    public void AddToOrder(Pizza wantedPizza)
    {
        orderList.Add(wantedPizza);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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