C# 用于存储自定义对象的自定义集合/列表
我有一个客户对象类:C# 用于存储自定义对象的自定义集合/列表,c#,list,class,C#,List,Class,我有一个客户对象类: public class customerObject { private string _address1; private string _address2; private string _address3; private string _category; private string _country; private string _county; private string _custcode
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; }
}
}
我有一门课
public class CustomerCollection
{
public List<customerObject> Customers { get; set; }
}
公共类CustomerCollection
{
公共列表客户{get;set;}
}
我的方法循环dt行并转换为customer对象
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;
}
公共列表目录(数据表名称)
{
//创建集合对象
Valueobjects.CustomerCollection集合=新的Valueobjects.CustomerCollection();
foreach(testablename.Rows中的DataRow行)
{
//创建客户对象
Valueobjects.customerObject Customer=新的Valueobjects.customerObject();
//设置客户对象的值
Customer.Rep_code=“”;
Customer.Int_rep_key=“”;
Customer.Int_rep_hou=“”;
Customer.Fullname=行[“Fullname”].ToString().Trim();
Customer.Custcode=行[“Custcode”].ToString().Trim();
Customer.Category=行[“Category”].ToString().Trim();
Customer.Address1=行[“Address1”].ToString().Trim();
Customer.Address2=行[“Address2”].ToString().Trim();
Customer.Address3=行[“Address3”].ToString().Trim();
Customer.Postcode=行[“Postcode”].ToString().Trim();
Customer.Country=行[“Country”].ToString().Trim();
Customer.Telephone=行[“电话”].ToString().Trim();
Customer.Lat=Convert.ToDouble(第[“Lat”行]);
Customer.Lng=Convert.ToDouble(第[“Lng”行]);
Customer.County=行[“County”].ToString().Trim();
//添加到集合(列表)
Collection.Customers.Add(客户);
}
TestableName=null;
回收;
}
但是,当我创建一个新的Customer对象和一个新的CustomerCollection对象时,我在将客户添加到集合列表时遇到了一个错误
错误:
错误32无法隐式转换类型
“Classes.Valueobjects.CustomerCollection”到
'System.Collections.Generic.List'
您的方法正在返回一个
列表
:
正如错误所说,这两种类型是不同的
如果CustomerCollection
已经是客户的集合,那么从语义上讲什么是列表
?收藏?似乎您的对象过于多元化:)
这里有两种方法。从以下方法返回CustomerCollection
:
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
如果要将通用列表用作收集容器,请使用列表
:
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
公共列表目录(数据表名称)
{
//...
var Collection=新列表();
//...
收集。添加(客户);
//...
回收;
}
旁注:对于变量命名,您可能希望坚持C#约定。从Stack Overflow上突出显示的代码中可以看出,变量名很容易被误认为类/类型,这可能会在支持代码时造成混淆。返回
CustomerCollection
而不是列表
:
您的对象有一个列表,它不是列表
MSDN:您的方法应该只返回
CustomerCollection
而不是列表
值对象。CustomerCollection
不是列表
,这是编译器告诉您的。顺便说一句,使用自动属性可以使代码看起来更好、更简洁。似乎没有必要使用只包含一个属性(即列表)的自定义类。为什么不直接传递列表?比如在方法中创建一个新列表并返回它?我如何将自定义列表放在viewstate中?@user4316519:除了“为什么要这样做?”这个问题之外,还有什么阻止你这么做的?您是指ASP.NET WebForms视图状态值吗?只要对象可以序列化,就可以正常工作。谢谢回复。用户将选择客户。这将被添加到一个列表中,该列表将填充GoogleMaps数据源,因此它需要在回发时保持。序列化对象以便在viewstate中使用是否容易?@user4316519:这听起来像是对AJAX请求或常规表单元素的很好的使用,而不一定是视图状态。特别是当JavaScript代码需要与数据交互时。视图状态只是数据的base-64编码序列化。如果JavaScript要与数据交互,跳过base-64部分将使其更容易。(除非视图状态自我上次看到它几年前以来发生了很大变化。)目前,对象看起来并不复杂。如果它没有序列化,那么这听起来像是堆栈溢出问题的一个很好的原因:)
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...