Home > OS >  C# Class Initialize and set data and List information
C# Class Initialize and set data and List information

Time:03-21

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.

CodePudding user response:

try to use

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

CodePudding user response:

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);
  • Related