IrvineCAGuy
IrvineCAGuy

Reputation: 211

C# Class Initialize and set data and List information

I am able to set the values for each item in ROOT, such as Address_to and Line_items, but when I try to pass the populated class to Post, it's empty.

public class OrdersClass
{
    public class Line_items
    {
        public string sku { get; set; }
        public int quantity { get; set; }

    }
    public class Address_to
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string address1 { get; set; }
        public string address2 { get; set; }
        public string city { get; set; }
        public string zip { get; set; }

    }
    public class Root
    {
        public string external_id { get; set; }
        public IList<Line_items> line_items { get; set; }
        public Address_to address_to { get; set; }
    }
}

My c# code:

 OrdersClass.Root thisOrder = new OrdersClass.Root();
 
 thisOrder.address_to = new OrdersClass.Address_to();

 IList<OrdersClass.Line_items> lineItems = new List<OrdersClass.Line_items>();

I can populate address_to as

  thisOrder.address_to.first_name  "my first name";

and line_items using: lineItems.Add(new OrdersClass.Line_items());

 lineItems[0].sku = ProductSKU;
 lineItems[0].quantity = cartQuantity;     

but..I know I'm doing this wrong.

Thanks.

Upvotes: 0

Views: 523

Answers (3)

IrvineCAGuy
IrvineCAGuy

Reputation: 211

Thanks to those who helped me get my thinking cap back on. Resolved. line_items.Add did not add the iList to the main "thisOrder" class. I had instantianted it syntactically correct, but it programmatically correct. This worked:

thisOrder.line_items = new List<OrdersClass.Line_items>();

Then adding a new ilist:

var lineItem = new OrdersClass.Line_items()
        {
            quantity = cartQuantity,
            sku = printifyProductSKU
        };
        thisOrder.line_items.Add(lineItem);

Yea. Now on the to the programming challenge:ASYNC Post vs Put. Thanks.

Upvotes: 0

Daniel
Daniel

Reputation: 9829

You need to add Line_items:

IList<OrdersClass.Line_items> lineItems = new List<OrdersClass.Line_items>();

var lineItem1 = new OrdersClass.Line_items()
{
    quantity = 1,
    sku = "sku1"
};
lineItems.Add(lineItem1);

var lineItem2 = new OrdersClass.Line_items()
{
    quantity = 2,
    sku = "sku2"
};
lineItems.Add(lineItem2);

Upvotes: 1

user6507856
user6507856

Reputation:

try to use

lineitems.Add(new OrderClass.Line_items(){
  sku = ProductSKU,
  quantity = cartQuantity
});

Upvotes: 0

Related Questions