[英]Custom Collection/List To Store Custom Objects
我有一個客戶對象類:
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'
您的方法返回一個List<CustomerCollection>
:
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//...
}
但是代碼試圖返回一個CustomerCollection
:
return Collection;
正如錯誤所言,這兩種類型是不同的。
如果CustomerCollection
已經是客戶的集合,那么從語義List<Valueobjects.CustomerCollection>
什么? 收藏集? 看來您過於復雜化了您的對象:)
這里有兩種方法。 從方法中返回一個CustomerCollection
:
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
如果要將通用列表用作集合容器,請使用List<Customer>
:
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
旁注:您可能要遵循C#約定進行變量命名。 從Stack Overflow上突出顯示的代碼可以看出,您的變量名很容易被誤認為是類/類型,在支持代碼時會引起混淆。
返回一個CustomerCollection
而不是List<Valueobjects.CustomerCollection>
:
public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...
您的對象有一個列表, 而不是列表。
MSDN: 繼承
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.