基本上,我试图询问用户产品的ID编号,然后将该编号的元素复制到新的集合中。将通用集合的元素复制到C中的另一个集合中#
我在这里添加了产品。
//Set up shop and add products
public void SetUpShop()
{
products.Add(new Farm_Shop(1, "Apple", "Fruit\t", 0.49, 40, 'n'));
products.Add(new Farm_Shop(2, "Orange", "Fruit\t", 0.59, 35, 'n'));
}
然后,我会显示一个菜单,允许用户键入他们想要的ID号码,例如,“Apple for Apple 1”。然后我试图做的是将“Apple”集合中的所有信息添加到新集合中。
//Display all items
public void DisplayAll()
{
Console.WriteLine("\nID\tProduct\tCategory\tPrice\tStock\tIn Basket?");
foreach (Farm_Shop temp in products)
{
//display each product to console by using Display method in Farm Shop class
temp.Display();
}
//Allow user to add a product to the basket (change 'inBasket' to 'y')
Console.WriteLine("Enter the ID of the item you wish to add to your basket: ");
int idToAdd = int.Parse(Console.ReadLine());
//Add array with the same as ID as person wants to add to basket to Basket array.
basket.Add //Trying to add element to the "basket" collection.
}
我能明显添加元素的集合,但它是事实,我必须得到的ID号码,然后比较,为什么在所有的阵列,然后采取阵列,以在它那个ID并将其添加到新集合中。
帮助赞赏,因为我一直在尝试这个多年现在。而且我刚刚意识到,我已经混淆了我在解释中转换数组和集合之间的关系。
谢谢。
我收到一个错误说: “无法隐式转换类型‘Farm_Shop_Assignment.Farm_Shop’到‘Farm_Shop_Assignment.Basket’” –
你可能已经建立的*篮*作为从* int *到* Basket *的字典。你不能把商店放在篮子里。 – galenus