【问题标题】:Custom Collection/List To Store Custom Objects自定义集合/列表来存储自定义对象
【发布时间】:2015-01-19 13:09:59
【问题描述】:

我有一个客户对象类:

  public class customerObject
  {
    private string _address1;
    private string _address2;
    private string _address3;
    private string _category;
    private string _country;
    private string _county;
    private string _custcode;
    private string _fullname;
    private string _int_rep_hou;
    private string _int_rep_key;
    private double _lat;
    private double _lng;
    private string _postcode;
    private string _rep_code;
    private string _telephone;

    public customerObject()
    {
    }

    public string Address1
    {
        get { return _address1; }
        set { _address1 = value; }
    }

    public string Address2
    {
        get
        {
            return _address2;
        }

        set { _address2 = value; }
    }

    public string Address3 { get { return _address3; } set { _address3 = value; } }

    public string Category
    {
        get { return _category; }
        set { _category = value; }
    }

    public string Country { get { return _country; } set { _country = value; } }

    public string County { get { return _county; } set { _county = value; } }

    public string Custcode
    {
        get { return _custcode; }
        set { _custcode = value; }
    }

    public string Fullname
    {
        get { return _fullname; }
        set { _fullname = value; }
    }

    public string Int_rep_hou
    {
        get { return _int_rep_hou; }
        set { _int_rep_hou = value; }
    }

    public string Int_rep_key
    {
        get { return _int_rep_key; }
        set { _int_rep_key = value; }
    }

    public double Lat { get { return _lat; } set { _lat = value; } }

    public double Lng { get { return _lng; } set { _lng = value; } }

    public string Postcode { get { return _postcode; } set { _postcode = value; } }

    public string Rep_code
    {
        get { return _rep_code; }
        set { Rep_code = value; }
    }
    public string Telephone { get { return _telephone; } set { _telephone = value; } 

 }
}

我有一个 CustomCollections 类

public class CustomerCollection
{

    public List<customerObject> Customers { get; set; }


}

我的方法循环遍历 dt 行并转换为客户对象

  public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
    {
        //Create Collection Object
        Valueobjects.CustomerCollection Collection = new Valueobjects.CustomerCollection();

        foreach (DataRow row in temptablename.Rows)
        {
            //Create Customer Object
            Valueobjects.customerObject Customer = new Valueobjects.customerObject();

            //set values of customer object
            Customer.Rep_code = "";
            Customer.Int_rep_key = "";
            Customer.Int_rep_hou = "";
            Customer.Fullname = row["Fullname"].ToString().Trim();
            Customer.Custcode = row["Custcode"].ToString().Trim();
            Customer.Category = row["Category"].ToString().Trim();
            Customer.Address1 = row["Address1"].ToString().Trim();
            Customer.Address2 = row["Address2"].ToString().Trim();
            Customer.Address3 = row["Address3"].ToString().Trim();
            Customer.Postcode = row["Postcode"].ToString().Trim();
            Customer.Country = row["Country"].ToString().Trim();
            Customer.Telephone = row["Telephone"].ToString().Trim();
            Customer.Lat = Convert.ToDouble(row["Lat"]);
            Customer.Lng = Convert.ToDouble(row["Lng"]);
            Customer.County = row["County"].ToString().Trim();

            //add to the collection (list)
            Collection.Customers.Add(Customer);
        }

        temptablename = null;

        return Collection;
    }

但是,当我创建一个新的 Customer 对象和一个新的 CustomerCollection 对象时,将客户添加到集合列表时出现错误。

错误:

错误 32 无法隐式转换类型 'Classes.Valueobjects.CustomerCollection' 到 'System.Collections.Generic.List'

【问题讨论】:

  • 你的方法应该只返回一个CustomerCollection而不是一个List&lt;CustomerCollection&gt;
  • Valueobjects.CustomerCollection 不是 List&lt;Valueobjects.CustomerCollection&gt;,这是编译器告诉你的。顺便说一句,使用自动属性让你的代码看起来更好更简洁。
  • 似乎没有必要有一个只包含一个属性的自定义类,那就是一个列表。为什么不直接传递列表?
  • 喜欢在方法中创建一个新列表然后返回这个?

标签: c# list class


【解决方案1】:

你的方法返回一个List&lt;CustomerCollection&gt;:

public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
    //...
}

但是代码试图返回一个CustomerCollection

return Collection;

正如错误所说,这两种类型是不同的。

如果CustomerCollection 已经是客户的集合,那么语义上什么是List&lt;Valueobjects.CustomerCollection&gt;?收藏的集合?看起来你的对象过度复数了:)

这里有两种方法。从方法中返回一个CustomerCollection

public CustomerCollection dolist(DataTable temptablename)
{
    //...
}

如果您想使用通用列表作为收集容器,请使用List&lt;Customer&gt;

public List<Customer> dolist(DataTable temptablename)
{
    //...
    var Collection = new List<Customer>();
    //...
    Collection.Add(Customer);
    //...

    return Collection;
}

旁注:您可能希望坚持 C# 约定来命名变量。正如您从 Stack Overflow 上突出显示的代码中看到的那样,您的变量名很容易被误认为是类/类型,这可能会在支持代码时造成混淆。

【讨论】:

  • 如何将自定义列表放入视图状态?
  • @user4316519:除了“你为什么想要?”的问题。是什么阻止你这样做?您的意思是 ASP.NET WebForms 视图状态值吗?只要对象可以被序列化,就可以正常工作。
  • 感谢您的回复。用户将选择客户。然后,这将被添加到一个列表中,该列表将填充谷歌地图数据源,因此它需要在回发时持续存在。序列化对象是否容易在视图状态中使用?
  • @user4316519:这听起来很好地使用了 AJAX 请求或常规表单元素,不一定是视图状态。特别是如果 JavaScript 代码需要与数据交互。视图状态只是数据的 base-64 编码序列化。如果 JavaScript 要与数据交互,跳过 base-64 部分会更容易。 (除非自几年前我上次看到它以来,视图状态发生了很大变化。)目前该对象看起来并不复杂。如果它没有序列化,那么这听起来像是 Stack Overflow 问题的一个很好的理由:)
【解决方案2】:

返回 CustomerCollection 而不是 List&lt;Valueobjects.CustomerCollection&gt;

public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...

你的对象一个列表,它不是一个列表。

MSDN:Inheritance

【讨论】: