简体   繁体   English

如何将一个类中的对象添加到另一个类中的List。

[英]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.

相关问题 如何从另一个类中访问一个类。 - How to access a class from within another class. 如何动态地将多个对象实例传递给另一个类。 - How to dynamically pass multiple object instance to another class. 创建抽象类的对象。 怎么可能 - Create object of abstract class. How possible 我有一个清单<transform>但我无法从另一个 class 访问。 我能做些什么?</transform> - I've an List<Transform> but i cannot access from another class. What can i do? 无法将新实例添加到通用类的通用列表中。 为什么? - Cannot add new instance to generic list in generic class. Why? 如何将对象添加到另一个类的列表中? - How do I add an object to a list in another class? 将数据存储在数组,对象,结构,列表或类中。 C# - Store data in array, object, struct, list or class. C# 从另一个类调用“this”函数。 什么是最好的方法? - Calling "this" function from another class. What is the best way? 无论如何,是否要将一个类的对象列表添加到另一个类的另一个对象列表中,该列表包含第一个类的实例作为它的属性? - Is there anyway to add an object list of a class to another object list of another class that contains instance of the first class as it's attribute? 从单独的 class 添加图表作为控件。 图表不画 - Add Chart as Control from separate class. Chart not drawing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM