Create a class with array of objects.

Posted by Bi on Stack Overflow See other posts from Stack Overflow or by Bi
Published on 2010-04-17T00:26:22Z Indexed on 2010/04/17 0:33 UTC
Read the original article Hit count: 223

Filed under:

Hi

Code below defines a ChargeCustomer class that contains an array of type "customers". I want to be able to create an object with either 1 "customer" or 2 "customers" based on the constructor parameters. Is this the right way to do so in C#:

public class ChargeCustomer 
{
    private Customer[] customers;

    public ChargeCustomer( string aName, string bName, int charge )
    {
        customers = new Customer[2];
        customers[0] = new Customer(aName, charge);
        customers[1] = new DropBox(bName, charge);  
    }

    public ChargeCustomer( string bName, int charge )
    {
        customers = new Customer[1];
        customers[0] = new Customer( bName, charge );
    }

}

Thanks!

© Stack Overflow or respective owner

Related posts about c#