tags:

views:

136

answers:

4

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?

+1  A: 

Is your confusion over how the method is returning an array?

It's just using List<T>.ToArray(). The list is built up one customer at a time, and then ToArray() is called at the end.

Jon Skeet
A: 

Hi,

I not sure what you would like to do here.

Do you want an Array of Customer? Or do you want to access the properties in Customer Class using an array type syntax?

to go through the code sample (2nd one)

the code makes an List of Customer,

creates a single customer which is populated via a DataReader (this repersents the Database data, which allows access via its column index or name)

the new cusomter is added to the List of customers

finally the List is converted into an Array (as its a List (Generic) it supports the conversion to a Customer[] with out the need of casting).

HTH Bones

dbones
+1  A: 

Like Jason says, you're just returning an array of Model.Customers from the List<Model.Customer>. It is possible to access a class like an array, and that's to use an indexer.

for example:

class Foo
{
     public string this [int index]
     {
          get { return new string; }
     }
}

This is then used like this:

Foo foo = new Foo();
string s = foo[0];

Obviously you would want to do something to make Foo return some values depending on index, but that's the basic way to do it.

Mystere Man
is there any spot answer? Your solution gives me idea but not exactly....
Phsika
I don't understand your question. What do you mean by a "spot answer"?
Mystere Man
+2  A: 

Are you asking how to convert a Customer object 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 List by adding Customers, then converted the List to 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 customer object you could also create an array called customers with one element and add a customer object to it like this:

Model.Customer[] customers = new Model.Customer[1];
customers[0] = customer;

or, an even shorter way:

Model.Customer[] customers = new Model.Customer[] {customer};
Steve Haigh