如何将类对象从一个组合框传递到另一个组合框?

时间:2015-01-09 00:29:26

标签: c# winforms class oop combobox

我有" System.NullReferenceException" - "对象引用不指示对象实例"

以下代码中的错误标记(//) (在产品类别中)

这里是我的classes.cs

我有麻烦将物品从一个组合框传递到另一个组合框。

public class Store { public string Name { get; set; }
public List<Product> Products { get; set; }

    public void AddProduct(Product p)
    {
        if (p != null && Products.Contains(p) == false)
        {
            Products.Add(p);
            p.AddStore(this);
        }
    }

}

public class Product
{
    public string Label {get; set;}
    public int Price { get; set; }
    public List<Store> Stores {get; set;}
    public void AddStore(Store b)
    {
        if (b != null && Stores.Contains(b) == false) // error here
        {
            Stores.Add(b);             // if i delete upper string, the error moves hereline
            b.AddProduct(this);
        }
    }

}

来自我的主要形式的代码:

FormP AP = new FormP(); if (AP.ShowDialog() == DialogResult.OK) { var newproduct = new Product { Label = AP.textBox1.Text, Price = Convert.ToInt32(AP.textBox2.Text) }; Store floor = AP.comboBox1.SelectedItem as Store;
AddProductsToStore(floor, newproduct); MessageBox.Show("New producr succesfully added"); }

和link-method:

public static void AddProductsToStore(Store store, Product product) { store.AddProduct(product); }

还有一个细节:comboBox1.DataSource = slist。我的代码中的slist是List

0 个答案:

没有答案