我不确定我是否正确地问这个问题,并且在这种情况下找不到我需要的东西。我有两节课。一个由我将分配的三个项目组成。
namespace Common.PriceFeed
{
public class SurchargeSKUList
{
public string WebSKUID { get; set; }
public decimal AdditionalPrice { get; set; }
public string Currency { get; set; }
}
}
另一个类是包含上述类中的项的列表。
namespace Common.PriceFeed
{
public class BaseSurcharge
{
public List<SurchargeSKUList> SKUList { get; set; }
}
}
我的问题是,然后我尝试使用BaseSurcharge,我得到的错误就像我使用的类型作为变量或我&#34;不能隐式转换类型...&#34; 我的.net代码如下。
谢谢。
if (BaseSurcharges.Rows.Count > 0)
{
foreach (DataRow row in BaseSurcharges.Rows)
{
SurchargeSKUList newSKUList = new SurchargeSKUList();
newSKUList.WebSKUID = row["WebSKUID"].ToString();
newSKUList.AdditionalPrice = Convert.ToDecimal(row["AdditionalPrice"]);
newSKUList.Currency = row["Currency"].ToString();
BaseSurcharge newSurcharge = new BaseSurcharge();
newSurcharge.SKUList = List<SurchargeSKUList>newSKUList;
//BaseSurcharge List<SurchargeSKUList> newSurcharge = new BaseSurcharge
//BaseSurcharge newSurcharge = new BaseSurcharge();
//newSurcharge.SKUList = newSKUList;
}
}
答案 0 :(得分:8)
newSurcharge.SKUList = List<SurchargeSKUList>newSKUList;
应更改为:
newSurcharge.SKUList = new List<SurchargeSKUList>();
newSurcharge.SKUList.Add(newSKUList);
我没有在Visual Studio上查看这个。所以它可能有一些语法错误。
但更好的是 - BaseSurcharge
在其构造函数中应该有SKUList = new List<SurchargeSKUList>();
。