[英]How to add object from a class to List in another class.
I am not sure I am asking this question properly and have had trouble searching for what I need in this case. 我不确定我是否正确地问了这个问题,并且在这种情况下无法找到所需的东西。 I have two classes. 我有两节课。 One consists of three items that I will assign. 其中一个由我要分配的三个项目组成。
namespace Common.PriceFeed
{
public class SurchargeSKUList
{
public string WebSKUID { get; set; }
public decimal AdditionalPrice { get; set; }
public string Currency { get; set; }
}
}
The other class is a list containing the items from the above class. 另一类是包含上述类中项目的列表。
namespace Common.PriceFeed
{
public class BaseSurcharge
{
public List<SurchargeSKUList> SKUList { get; set; }
}
}
My problem is, then I try to use BaseSurcharge, I get errors like I am using as type as a variable or that I "cannot implicitly convert type..." My .net code is below. 我的问题是,当我尝试使用BaseSurcharge时,出现诸如将类型用作变量或“无法隐式转换类型...”这样的错误。我的.net代码如下。
Thank you. 谢谢。
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;
}
}
newSurcharge.SKUList = List<SurchargeSKUList>newSKUList;
should be changed to: 应该更改为:
newSurcharge.SKUList = new List<SurchargeSKUList>();
newSurcharge.SKUList.Add(newSKUList);
I didn't check this out on Visual Studio. 我没有在Visual Studio上进行检查。 So it might have some syntax errors. 因此它可能有一些语法错误。
But better yet - BaseSurcharge
should have SKUList = new List<SurchargeSKUList>();
但更好的是BaseSurcharge
应该具有SKUList = new List<SurchargeSKUList>();
in its constructor. 在其构造函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.