简体   繁体   中英

Access Array List inside a public class

Not the duplicate of Add objects to arraylist and read them I could access the data and read them in json format but not in orderItem.PO , etc format. And also my problem is not "How to read".

I have a List<xmlOrder> items which contain orders information and their items. I can access the list like following:

    foreach (var order in items)
    {
       string query = "insert into Orders values ('" + order.PO + "','" + order.Name + "','" + order.Company + "','" + order.Address + "','" + order.City + "','" + order.State + "','" + order.ZipCode + "','" + order.Country + "','" + order.Phone + "','" + order.Date + "','" + order.ShippingMethod + "','" + order.Notes + "')";
       foreach (var orderItem in order.OrderItems)
       {
        query = "insert into OrderItem values ('" + orderItem.PO + "','" + orderItem.StockNumber + "','" + orderItem.Quantity + "','" + orderItem.UnitPrice + "')";
       //Console.WriteLine(orderItem.ToJson());
       }
    }

I can access order.PO, order.Name , etc but I get error for orderItem like below: 在此处输入图片说明

The weird part is when I check orderItem.ToJson() I get the json fine like:

{"PO":"009723","StockNumber":"0040","Quantity":5,"UnitPrice":16.445000}

Which means I am getting the data but why is it giving me error when I try to access the data like orderItem.PO?

My class structure is like below if needed:

public class xmlOrder
{

    public string PO { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Date { get; set; }
    public string ShippingMethod { get; set; }
    public string Notes { get; set; }
    public ArrayList OrderItems { get; set; }

    public xmlOrder()
    {
        this.PO = string.Empty;
        this.Name = string.Empty;
        this.Company = string.Empty;
        this.Address = string.Empty;
        this.City = string.Empty;
        this.State = string.Empty;
        this.ZipCode = string.Empty;
        this.Country = string.Empty;
        this.Phone = string.Empty;
        this.Date = string.Empty;
        this.ShippingMethod = string.Empty;
        this.Notes = string.Empty;
        this.OrderItems = new ArrayList();
    }
}

public class xmlOrderItem
{
    public string PO { get; set; }
    public string StockNumber { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }

    public xmlOrderItem()
    {
        this.PO = string.Empty;
        this.StockNumber = string.Empty;
        this.Quantity = 0;
        this.UnitPrice = decimal.Zero;
    }
}

What is the type of your items object? You should show more of this method. If it's of type object you should try to make it a concrete object. You could also cast your object to your type as such:

foreach (var orderItem in order.OrderItems)
   {
      var item = orderItem as xmlOrderItem
      if (item == null)
        throw new NullReferenceException("orderItem is not an xmlOrderItem")
      query = "insert into OrderItem values ('" + item .PO + "','" + item .StockNumber + "','" + item .Quantity + "','" + item .UnitPrice + "')";
   //Console.WriteLine(orderItem.ToJson());
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM