解决 C# 错误:'SemiItemPriceBOM' 属性未初始化或为空
在 C# 代码中,当尝试将 FormLinegroupBOM 对象添加到 SemiItemPriceBOM 列表时,可能会遇到如下错误:
FormLinegroupBOM fl = new FormLinegroupBOM();
// 查询后赋值
//items.Itemcode=aaa.Itemcode
fl.Itemcode = item.Itemcode;
fl.Bomitemcode = bom.Itemcode;
fl.Dosage = bom.Dosage;
fl.Unitprice = (float)unitPrice;
fl.Price = fl.Dosage * fl.Unitprice;
fl.Itemmodel = bom.Itemmodel;
fl.Itename = bom.Itename;
fl.Unit = bom.Unit;
item.SemiItemPriceBOM.Add(fl);
这个错误通常是由于 item.SemiItemPriceBOM 属性没有被初始化或为空造成的。
在将 fl 对象添加到 SemiItemPriceBOM 列表之前,需要检查该列表是否为空,并进行初始化。
// 检查 SemiItemPriceBOM 列表是否为空,如果为空,则初始化它
if (item.SemiItemPriceBOM == null)
{
item.SemiItemPriceBOM = new List<FormLinegroupBOM>();
}
// 现在可以将 fl 对象添加到列表中
item.SemiItemPriceBOM.Add(fl);
通过以上方法,可以确保在添加对象之前,SemiItemPriceBOM 列表已经存在,从而避免错误的发生。
原文地址: https://www.cveoy.top/t/topic/mqm9 著作权归作者所有。请勿转载和采集!