views:

92

answers:

4

Ok I have a class similar to the following...

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

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

}

public class Items
{
    private string itemName;
    [DataMember]
    public string ItemName
    {
        get { return itemName; }
        set { itemName = value; }
    }

}

When I reference in my code I have a method that takes in an "Order" list as the parameter.

ACME.Order newOrder = new ACME.Order();
ACME.Items newItems = new ACME.Items();

newOrder.ID = xxx
newItems.ItemName = xxx

SendOrderWithItemsFunction(newOrder)

The above works fine however I don't have an add function for my items so that I can do something like the following

newOrder.Items.Add(newItem);

and

newOrder.Items = newItems

will not work because it says that it can not implicitly convert newOrder.Items to newItems[].

What am Missing?

A: 

You should be able to do:

newOrder.OrderItems.Add(newItem);

If your newItems[] is an array, you need to do this:

newOrder.OrderItems.AddRange(newItem.ToList<Items>());
Rashmi Pandit
A: 

You have declared newItems as an ACME.Items type, but the OrderItems property of your Order class is a List<Items>. Those types are not assignable from one to the other directly. So, an assignment of newOrder.OrderItems = newItems is like trying to sayList<Items> = Items. That isn't possible based on the classes you outlined above. Instead, you will need to add to the list.

jheddings
A: 

What you do have is an add function in your Order.OrderItems property:

newOrder.OrderItems.Add(newItem);

you can even add a whole list of items to your OrderItems:

var someList = new List<Items>();
//populate someList here
newOrder.OrderItems.AddRange(someList);
Jon Limjap
+2  A: 

I think I might be missing something, but newOrder.OrderItems.Add(newItem) should work just fine, according to waht you have in your post.

Just some other nitpick things:

The pluralization of the "Items" class is wierd, if it is only a single Item. This is probably the reason that it looked "ok" to assign a single item to a List property.

You may have cut it out of your post, but every class that is being serialized by WCF must be marked as a "DataContract", not just the members of the class.

When initializing objects like this, I think it makes it a lot cleaer to use Type Initializers:

var NewOrder = new ACME.Order{
     ID = xxx,
     OrderItems = new List<ACME.Item>
     {
          new ACME.Item{
               ItemName = xxx
          }
     }
};
Chris
+1 for picking up on the weird pluralization...
IanR
Great point about the pluralization, I'll definitely make that change. Also I do have the datacontract information I just cut it out as you said to keep the example clean. However my issue is that I don't have the Add function available to me in intelisense. All I have are things like Any<>, Aggregate<> and so on which leads me to believe that I am missing something but I'm not sure what.
Kuruption
nevermind got it. Thanks.
Kuruption