0

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: enter image description here

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;
    }
}
5
  • 3
    Why are you using ArrayList? Consider switching to List<OrderItem>. Commented Apr 15, 2019 at 21:38
  • Possible duplicate of Add objects to arraylist and read them Commented Apr 15, 2019 at 21:39
  • 1
    @tukaef THANKS! used list instead of ArrayList and it worked :) Commented Apr 15, 2019 at 21:53
  • It is a duplicate because the issue is the same: when using foreach to enumerate an ArrayList you are unable to access members from a derived type because the iteration variable is of type object. Did you read the solution presented in the accepted answer and try applying it to your code? It's the same idea as in @Jonathan's answer here. I see that changing from ArrayList to List<> solved your problem, which also happens to be the same solution proposed in the other answer on that question. Commented Apr 16, 2019 at 17:03
  • Also, there is no ToJson() method in your xmlOrderItem class, so it must be an extension method. It makes sense that a method that converts arbitrary objects to JSON would take an object parameter, therefore you could call that method on any variable of any type, including your variable orderItem. Being able to call an object method (extension or otherwise) doesn't indicate anything about the compile-time type of a variable, which in this case is obscured by the use of var. Commented Apr 16, 2019 at 17:24

1 Answer 1

1

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 objectto 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());
   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.