I was searcing for some information, and I found a method like this:
public partial class Customer {
private string customerIDField;
private string companyNameField;
private string contactNameField;
private string contactTitleField;
private string addressField;
private string cityField;
private string regionField;
private string postalCodeField;
private string countryField;
private string phoneField;
private string faxField;
// Other properties
}
Then the coder used this class like this. How?
private static Model.Customer[] BuildCustomers(SqlDataReader reader)
{
if (reader.HasRows)
{
List<Model.Customer> custs = new List<Model.Customer>();
while (reader.Read())
{
Model.Customer cust = new Model.Customer();
cust.CustomerID = reader["CustomerID"].ToString();
cust.CompanyName = reader["CompanyName"].ToString();
cust.ContactName = reader["ContactName"].ToString();
custs.Add(cust);
}
return custs.ToArray();
}
return null;
}
I really want to learn how this coder uses “Customer —–> Model.Customer[] “. Is there any method that does this?
Are you asking how to convert a
Customerobject in to an array?There are a couple of ways to do it – the example you showed in the second piece of code built up a
Listby addingCustomers, then converted theListto an array. This works well when you do not know how many customers you have, you keep adding to the list and create and array at the end and the array will be the correct size.If you just have one
customerobject you could also create an array calledcustomerswith one element and add acustomerobject to it like this:or, an even shorter way: