0

I have stored one arraylist list into 2nd arraylist and a third arraylist contains 2nd arraylist list.

deliveries arraylist contains list of product and each product list contain list of productdetails.

Now how to get the first productdetails list in deliveries arraylist.

I tried this but not working

Let suppose I want first enter in productdetails arraylist.

( (ArrayList)deliveries0[0] )[0].GetValue(1).ToString()

6
  • 3
    ArrayList is old, you should use List<T> instead, and your structure should be a list of custom classes (e.g. delivery) having a Property that is a list of other classes (e.g. product) and so on... Commented Apr 15, 2011 at 7:49
  • Unless you are on .Net 1, there's no reason to use ArrayList. List<T> provides everything ArrayList does plus type-safety. Commented Apr 15, 2011 at 7:51
  • +1 @digEmAll for custom Class usage Commented Apr 15, 2011 at 7:57
  • My question is about casting. Commented Apr 15, 2011 at 9:23
  • @Zain: "i tried this but not working" what's the error then ? Commented Apr 15, 2011 at 13:08

2 Answers 2

1

You can also use LINQ to get the record.

var detail = deliveries.FirstOrDefault().ProductDetails.FirstOrDefault(); 
Sign up to request clarification or add additional context in comments.

Comments

1

It is often easier to model things as objects:

    public class Delivery
    {
        public List<Product> Products { get; set; }
        public Delivery()
        {
            Products = new List<Product>();
        }
    }

    public class Product
    {
        public List<ProductDetail> ProductDetails { get; set; }
        public Product()
        {
            ProductDetails = new List<ProductDetail>();
        }
    }

    public class ProductDetail
    {
        public string Summary { get; set; }
        public string Details { get; set; }
    }

You can then create a delivery, product and product description like this:

    Delivery delivery = new Delivery();
    Product product = new Product();
    ProductDetail detail = new ProductDetail();
    delivery.Products.Add(product);
    product.ProductDetails.Add(detail);

Retrieving the product details is then as simple as:

    ProductDetail detail = delivery.Products[0].ProductDetails[0];

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.