tags:

views:

124

answers:

4

I have the following classes...

public class Order
{
    private Guid id;
    public Guid ID
    {
        get { return id; }
        set { id = value; }
    }

    private List<Items> orderItems;
    public List<Items> OrderItems
    {
        get { return orderItems; }
        set { orderItems= value; }
    }

}

public class Item
{

    private Guid id;
    public Guid ID
    {
        get { return id; }
        set { id = value; }
    }

    private string itemName;
    public string ItemName
    {
        get { return itemName; }
        set { itemName = value; }
    }

}

Then within my application I try the following....

ACME.Order newOrder = new ACME.Order();
newOrder.ID = xxx
newOrder.OrderItems = new List<OrderItem> {
     new ACME.OrderItem {
         ID = xxx
         ItemName = xxx
     }
}

However I get the error "Cannot implicitly convert type 'System.Collections.Generic.List<ACME.Item>' to ACME.Item[]. The strange thing as well is that all I don't have an "Add" option on any of my list objects.

+1  A: 

Is your application on the client side of a Web service?

If so, the proxy generator for .asmx and WCF Web services generates an array T[] on the client side where a service interface uses a List<T> (or, indeed, any other enumerated type).

Your application will need to cast the list to an array (use .ToArray()) to set the array property client-side.

Jeremy McGee
Good spot - also note that there are options to use `List<T>` etc; `/collectionType` for svcutl.exe, etc
Marc Gravell
Thanks that worked just added the collection type line from svcutil and all works.
Kuruption
A: 

I'm assuming that List<Items> is a typo, and you mean List<Item> ? And is OrderItem : Item? Or is this another typo?

I'm making the assumption that OrderItem : Item...

With public List<Item> OrderItems {...}, it will have to be a List<Item>:

newOrder.OrderItems = new List<Item> {
     new ACME.OrderItem {
         id = xxx
     }
}

since lists aren't covariant, even in 4.0

Marc Gravell
A: 

Ignoring typos I would recommend turning your generic list on Order to auto initialize. Then you just always deal with the list directly instead of worrying about initialization when adding items. So something like:

    public class Order
    {
        public Guid ID { get; set; }
        private List<Item> orderItems;
        public List<Item> OrderItems
        {
            get 
            {
                if (orderItems == null)
                    orderItems = new List<Item>();
                return orderItems; 
            }
        }
    }
    public class Item
    {
        public Guid ID { get; set; }
        public string ItemName { get; set; }
    }

Then in your application you would do something like:

        Order newOrder = new Order { ID = new Guid() };
        newOrder.OrderItems.Add( new Item() { ID = new Guid(), ItemName = "New Item"});
Firestrand
A: 
newOrder.OrderItems = new List<OrderItem> {
     new ACME.OrderItem {
         ID = xxx
         ItemName = xxx
     }

Ignoring your typos, your new List should be created like

newOrder.OrderItems = new List<OrderItem>() { new OrderItem { IX=1,Itemname="somename}}

I saw that you were missing the "()" after new List<OrderItem>

HTH

ram