C# syntax

M

Mike P

I am playing around with the new C# features, but I am having problems
with initialising an object that contains a collection within it (eg a
customer with several order - see below). Can anybody tell me the
correct syntax?

public List<Customer> GetCustomerList()
{
List<Customer> customerList = new List<Customer>
{
new Customer
{
ID = "ALFKI", Name = "Alfreds Futterkiste", Address =
"Obere Str. 57", City = "Berlin", PostalCode = "12209", Country =
"Germany", Phone = "030-0074321", Fax = "030-0076545",
Orders =
new Order
{
ID = 10643, OrderDate =
Convert.ToDateTime("25/08/1997"), Total = 814.50
}
}
};

return customerList;
}
 
P

Pavel Minaev

I am playing around with the new C# features, but I am having problems
with initialising an object that contains a collection within it (eg a
customer with several order - see below).  Can anybody tell me the
correct syntax?

Since you don't give the definition of class Customer, I'll have to
guess.
public List<Customer> GetCustomerList()
    {
        List<Customer> customerList = new List<Customer>
        {
            new Customer
            {
                ID = "ALFKI", Name = "Alfreds Futterkiste", Address =
"Obere Str. 57", City = "Berlin", PostalCode = "12209", Country =
"Germany", Phone = "030-0074321", Fax = "030-0076545",
                Orders =
                new Order
                {
                    ID = 10643, OrderDate =
Convert.ToDateTime("25/08/1997"), Total = 814.50
                }

Assuming that Orders is a property of Customer of type List<Order>,
you need to do this:

Orders = new List<Order> { new Order { ... } }

As it is, you're trying to assing a value of type Order to a property
 
M

Mr. Arnold

Mike P said:
I am playing around with the new C# features, but I am having problems
with initialising an object that contains a collection within it (eg a
customer with several order - see below). Can anybody tell me the
correct syntax?

I don't know about the rest of the code, but the List needs a constuctor on
the *new* part doesn't it?

List<Customer> customerList = new List<Customer>();
 
H

Hans Kesting

Mike P has brought this to us :
I am playing around with the new C# features, but I am having problems
with initialising an object that contains a collection within it (eg a
customer with several order - see below). Can anybody tell me the
correct syntax?

public List<Customer> GetCustomerList()
{
List<Customer> customerList = new List<Customer>
{
new Customer
{
ID = "ALFKI", Name = "Alfreds Futterkiste", Address =
"Obere Str. 57", City = "Berlin", PostalCode = "12209", Country =
"Germany", Phone = "030-0074321", Fax = "030-0076545",
Orders =
new Order
{
ID = 10643, OrderDate =
Convert.ToDateTime("25/08/1997"), Total = 814.50
}
}
};

return customerList;
}

A guess: would "Orders" (property of Customer) be a List<Order>? You
set it to a single Order.

An extra remark:
Maybe it's better to initialize the OrderDate through a direct call to
the constructor: OrderDate = new DateTime(1997, 8, 25)
Then you wouldn't have to worry about D/M/Y versus M/D/Y format.

Hans Kesting
 
M

Mike P

I have tried doing this :

List<Customer> customerList = new List<Customer>
{
new Customer
{
ID = "ALFKI", Name = "Alfreds Futterkiste", Address =
"Obere Str. 57", City = "Berlin", PostalCode = "12209", Country =
"Germany", Phone = "030-0074321", Fax = "030-0076545",
Orders = new List<Order>
{
new Order
{
ID = 10643, OrderDate =
Convert.ToDateTime("25/08/1997"), Total = 814.50
}
}
}
};

and this :

List<Customer> customerList = new List<Customer>
{
new Customer
{
ID = "ALFKI", Name = "Alfreds Futterkiste", Address =
"Obere Str. 57", City = "Berlin", PostalCode = "12209", Country =
"Germany", Phone = "030-0074321", Fax = "030-0076545",
Orders =
List<Order> orderList = new List<Order>
{
new Order
{
ID = 10643, OrderDate =
Convert.ToDateTime("25/08/1997"), Total = 814.50
}
}
}
};

But I still get an error...what am I doing wrong?
 
M

Mike P

By the way, this is my customer class :

public class Customer
{
public Customer(string strID, string strName, string strAddress,
string strCity, string strPostalCode,
string strCountry, string strPhone, string strFax,
List<Order> lstOrders)
{
this.ID = strID;
this.Name = strName;
this.Address = strAddress;
this.City = strCity;
this.PostalCode = strPostalCode;
this.Country = strCountry;
this.Phone = strPhone;
this.Fax = strFax;
this.Orders = lstOrders;
}

public string ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public List<Order> Orders { get; set; }
}

And this is my Order class :

public class Order
{
public Order(int intID, DateTime dtmOrderDate, double dblTotal)
{
this.ID = intID;
this.OrderDate = dtmOrderDate;
this.Total = dblTotal;
}

public int ID { get; set; }
public DateTime OrderDate { get; set; }
public double Total { get; set; }
}
 
P

Pavel Minaev

By the way, this is my customer class :

public class Customer
{
    public Customer(string strID, string strName, string strAddress,
string strCity, string strPostalCode,
                    string strCountry, string strPhone, string strFax,
List<Order> lstOrders)
        {
        this.ID = strID;
        this.Name = strName;
        this.Address = strAddress;
        this.City = strCity;
        this.PostalCode = strPostalCode;
        this.Country = strCountry;
        this.Phone = strPhone;
        this.Fax = strFax;
        this.Orders = lstOrders;
        }

    public string ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public List<Order> Orders { get; set; }

}

And this is my Order class :

public class Order
{
    public Order(int intID, DateTime dtmOrderDate, double dblTotal)
        {
        this.ID = intID;
        this.OrderDate = dtmOrderDate;
        this.Total = dblTotal;
        }

    public int ID { get; set; }
    public DateTime OrderDate { get; set; }
    public double Total { get; set; }

}

This explains things. Both of your classes don't have any default
constructor, since you've explicitly defined a constructor with
arguments for them (the compiler only generates an empty argumentless
default constructor for you if you do not define any constructors in
the class explicitly). When you use the object initializer the way you
did - new Order { ... } - it is equivalent to using the default
constructor - new Order() { ... }. Since you don't have one, you get
the error. You should explicitly declare a default constructor in both
of your classes (or remove your other one) if you want it to compile.

By the way, in the future, when your question involves references to a
compiler error, please always give the complete text of the error you
receive, and the line on which it happens (preferrably with some
context).
 
G

Guest

I think it has to be sometihing like this:

class Order
{
private int _intID;
private DateTime _orderdate;
private double _total;

public Order(int intID, DateTime dtmOrderDate, double dblTotal)
{
this._intID = intID;
this._orderdate = dtmOrderDate;
this._total = dblTotal;
}

public int ID
{
get { return this._intID; }
set { this._intID = value; }
}

public DateTime OrderDate
{
get { return this._orderdate; }
set { this._orderdate = value; }
}

public double Total
{
get { return this._total; }
set { this._total = value; }
}
}

HTH
 
M

Mike P

Thanks everybody...adding the default constructors allowed me to use the
following syntax :

List<Customer> customerList = new List<Customer>
{
new Customer
{
ID = "ALFKI", Name = "Alfreds Futterkiste", Address =
"Obere Str. 57", City = "Berlin", PostalCode = "12209", Country =
"Germany", Phone = "030-0074321", Fax = "030-0076545",
Orders = new List<Order>
{
new Order
{
ID = 10643, OrderDate =
Convert.ToDateTime("25/08/1997"), Total = 814.50
},
new Order
{
ID = 10692, OrderDate =
Convert.ToDateTime("03/10/1997"), Total = 878.00
}
}
}


Cheers,

Mike
 
G

Göran Andersson

Mike said:
Thanks everybody...adding the default constructors allowed me to use the
following syntax :

List<Customer> customerList = new List<Customer>
{
new Customer
{
ID = "ALFKI", Name = "Alfreds Futterkiste", Address =
"Obere Str. 57", City = "Berlin", PostalCode = "12209", Country =
"Germany", Phone = "030-0074321", Fax = "030-0076545",
Orders = new List<Order>
{
new Order
{
ID = 10643, OrderDate =
Convert.ToDateTime("25/08/1997"), Total = 814.50
},
new Order
{
ID = 10692, OrderDate =
Convert.ToDateTime("03/10/1997"), Total = 878.00
}
}
}


Cheers,

Mike

Why on earth don't you use the constructors that you already have?

List<Customer> customerList = new List<Customer> {
new Customer(
"ALFKI", "Alfreds Futterkiste",
"Obere Str. 57", "Berlin", "12209", "Germany",
"030-0074321", "030-0076545",
new List<Order> {
new Order(10643, new DateTime(1997,8,25), 814.50),
new Order(10692, new DateTime(1997,10,3), 878.00)
}
)
}
 
G

Göran Andersson

b said:
I think it has to be sometihing like this:

class Order
{
private int _intID;
private DateTime _orderdate;
private double _total;

public Order(int intID, DateTime dtmOrderDate, double dblTotal)
{
this._intID = intID;
this._orderdate = dtmOrderDate;
this._total = dblTotal;
}

public int ID
{
get { return this._intID; }
set { this._intID = value; }
}

8< snip

No, it doesn't. There is a new short form in C# 3 for writing parameters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top