为什么不能将对象添加到列表中?
本文关键字:列表 添加 不能 对象 为什么 | 更新日期: 2023-09-27 18:08:37
(获取底部的所有c#文件)
我已经在我的类"菜单"中创建了一个pizza对象列表。当我试图将pizza类的对象添加到列表(再次来自menu类)时,我遇到了一个问题:
(顺便说一句,我曾试图翻译,但我很抱歉-其中一些是丹麦语作为作业的一部分-但上面的评论是翻译)
//add to menu
public void TilføjTilMenukort()
{
//"add element to the menu?"
Console.WriteLine("Tilføj element til menuen? j/n");
svar = char.Parse(Console.ReadLine());
if(svar == 'j')
{
//enter number
Console.WriteLine("Indtast nummer");
int newNr = int.Parse(Console.ReadLine());
//enter name
Console.WriteLine("'nIndtast navn");
string newNavn = Console.ReadLine();
//enter ingredient
Console.WriteLine("'nIndtast ingredienser adskilt med ','");
string newIngrediens = Console.ReadLine();
//enter price
Console.WriteLine("'nIndtast pris");
double newPris = double.Parse(Console.ReadLine());
//list name is "pizza on menu"
PizzaPaaMenu.Add(new Pizza(newNr, newNavn, newIngrediens, newPris));
}
}
就好像对象被添加到列表中,因为在此之后,我调用了一个方法,该方法写入列表的每个元素-并且它出现在列表中!:
//show menu
public void VisMenu()
{
Console.WriteLine("Menu:'n");
foreach (Pizza p in PizzaPaaMenu)
{
Console.WriteLine("{0}. {1} - {2} - {3} kr.'n", p.Nr, p.PizzaNavn, p.Ingrediens, p.Pris);
}
}
但是当我试图从另一个类(排序)之后运行另一个方法时-它一直返回说没有找到披萨的行:
Console.WriteLine("'nPizzaen med nr. " + bestilNummer + " blev ikke fundet!");
. .在下面这段代码中找到:
//create order
public void OpretBestilling()
{
do
{
//"which number from the menu do you want?"
Console.WriteLine("'nHvilket nummer fra menuen ønsker du at bestille?");
//orderNumber
int bestilNummer = int.Parse(Console.ReadLine());
//orderPizza
Pizza bestiltPizza = null;
Menukort menukort = new Menukort();
//The pizzas is searched through for the pizza with number "bestilNummer"("orderNumber")),
foreach (Pizza p in menukort.PizzaPaaMenu)
{
if (p.Nr == bestilNummer)
{
//if it is found in the list "PizzaPaaMenu", "bestiltPizza" points to the pizza being added (pizza p)
bestiltPizza = p;
break;
}
}
//if it is a "real" pizza (not null / p), it gets added to the list "BestillingsListe" ("order-list")
if (bestiltPizza != null)
{
BestillingsListe.Add(bestiltPizza);
//"the pizza is added to your order"
Console.WriteLine("'n" + bestiltPizza.PizzaNavn + " er tilføjet til bestillingen");
//"do you wish to order more?"
Console.WriteLine("'nØnsker du at bestille mere? j/n");
done = char.Parse(Console.ReadLine());
}else{
//"Pizza x was not found"
Console.WriteLine("'nPizzaen med nr. " + bestilNummer + " blev ikke fundet!");
}
} while (done == 'j');
}
Main方法中的重要代码如下所示:
//should add a pizza-object to the list of pizzas in "menu"
menukort.TilføjTilMenukort();
//runs through the list of pizza-objects
menukort.VisMenu();
//"do you wish to order?"
Console.WriteLine("'nØnsker du at bestille? j/n");
charsvar = char.Parse(Console.ReadLine());
if (charsvar == 'j')
{ //creates an order by making sure, that the pizza exists in the list from "menu"
bestilling.OpretBestilling();
//shows what has been ordered
bestilling.VisBestilling();
…
谢谢!
获取所有文件
Bestilling
类不知道您在主程序中使用的Menukort
实例。它创建了自己的实例,这是一个独立的比萨饼列表,与您之前一直使用的列表不同。
不是在Bestilling
类中创建Menukort
的新实例,而是将您使用的实例发送到它:
Menukort menukort = new Menukort();
Bestilling bestilling = new Bestilling(menukort);
在Bestilling
类中为它声明一个变量:
class Bestilling
{
private Menukort menukort;
...
在构造函数中,您获得对实例的引用并放入变量:
public Bestilling(Menukort kort) {
menukort = kort;
}
删除创建新实例的行(342),其余的代码将是相同的,使用您添加到类中的变量。
为我们的英国观众翻译:
Bestilling = Food order
菜单表