我正在尝试创建我的巧克力类的新实例,然后将其放入列表中。当我在控制台中查看newChocolate时,我收到错误:对象引用未设置为对象的实例。我在这里看到了类似的问题,答案似乎总是需要实例化,当我说:Chocolate newChocolate = new Chocolate
Chocolate newChocolate = new Chocolate(comboBoxChocolateSelection.SelectedItem.ToString(), 12.5, true, 2);
m_model.AddChocolateInList(newChocolate);
我的选择框显示为在控制台中工作,但在查看newChcolate时显示为null。
Console.WriteLine("flavor selected : " + comboBoxChocolateSelection.SelectedItem.ToString());
Prints =>选择的味道:黑暗
Console.WriteLine("newChocolate : " + newChocolate.flavor.ToString());
prints => newChocolate。味道为空。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GatesCandy
{
public class Chocolate
{
#region Fields
public string flavor;
public double cost;
public bool giftWrap;
public int quantity;
#endregion End of Fields
#region Constructors
public Chocolate(string flavor, double cost, bool giftWrap, int quantity)
{
Flavor = flavor;
Cost = cost;
GiftWrap = giftWrap;
Quantity = quantity;
}
#endregion End of Constructors
#region Properties
public string Flavor { get; set; }
public double Cost { get; set; }
public bool GiftWrap { get; set; }
public int Quantity { get; set; }
#endregion End Properties
}
}