从类中的集合字段获取值

时间:2013-02-15 18:16:52

标签: c# string collections

我需要从属性/字段中获取集合类型数据并最终获得 将其显示在表单上。我该怎么做?

下面的代码解释了我想要做的和我得到的错误,希望它有意义。

public class C
{

    public string Id { get; set; }
    //public List<string> ColVal { get; }

    public C()
    {}

    public C(ObjectA objVar)
    {
        Id = objVar.Id;
        //ColVal = objVar.ColVals; //<- Errors out: This is a collection type property, how do I get values & solve this?
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道ObjectA是什么,但它看起来应该是C类,所以我改变了。然后我注意到你的集合属性没有setter。

公共类C {

public string Id { get; set; }
public List<string> ColVal { get; set; }

public C()
{}

public C(C objVar)
{
    Id = objVar.Id;
   ColVal = objVar.ColVal; //<- Errors out: This is a collection type property, how do I get values & solve this?
}
}
相关问题