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;
}
}
ArrayList? Consider switching toList<OrderItem>.foreachto enumerate anArrayListyou are unable to access members from a derived type because the iteration variable is of typeobject. 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 fromArrayListtoList<>solved your problem, which also happens to be the same solution proposed in the other answer on that question.ToJson()method in yourxmlOrderItemclass, so it must be an extension method. It makes sense that a method that converts arbitrary objects to JSON would take anobjectparameter, therefore you could call that method on any variable of any type, including your variableorderItem. Being able to call anobjectmethod (extension or otherwise) doesn't indicate anything about the compile-time type of a variable, which in this case is obscured by the use ofvar.