Mr.Bhanushali
Mr.Bhanushali

Reputation: 126

How to add object to a list which contains another list

the problem is , I want to achieve the structure like this:

Category > SubCategory > Product

the scenario will be like , multiple products will be added to sub category, and multiple sub categories will be added to category. I am having very less knowledge, so my question is can we do that, if yes the please help me to achieve that.

Here is my Code:

Category obj = new Category();
            obj.CategoryId = categoryId++;
            obj.CategoryName = Console.ReadLine();

            do{
                Console.WriteLine("want to add SubCategory ----(Y/N)");
                string loop = Console.ReadLine();

                if (loop == "Y" || loop == "y")
                {
                    List<SubCategory> subcategories = new List<SubCategory>();
                    SubCategory sub = new SubCategory();

                    sub.CategoryId = obj.CategoryId;
                    sub.SubCategoryId = subcategoryId++;
                    sub.SubCategoryName = Console.ReadLine();
                    do
                    {
                        Console.WriteLine("want to add product ----(Y/N)");
                        string loop1 = Console.ReadLine();

                        if (loop1 == "y" || loop == "Y")
                        {

                            List<Product> products = new List<Product>();
                            Product product = new Product();
                            Product product1 = new Product();
                            Console.Write("Name :");
                            product.ProdictId = productId++;
                            product.ProdictName = Console.ReadLine();
                            Console.Write("Price :");
                            product.ProductPrice = Convert.ToDouble(Console.ReadLine());

                            product.SubCategoryId = sub.SubCategoryId;

                            sub.Products.Add(product);
                        }
                        else { break; }
                        sub.Products.Add(pro)
                    } while (true);


                    obj.SubCategories.Add(sub);
                }
                else { break; }

            } while (true);
            categories.Add(obj); 
        }

P.S. here categoryId, SubCategoryId, and ProductId is set as auto-increment.

How can i add multiple products to Subcategory, and multiple sub category to category.

it will be a big help. thank you in advance.

Upvotes: 2

Views: 198

Answers (2)

Chetan
Chetan

Reputation: 6891

The issue with your code is that the collection properties such as SubCategories property of object of Category class and Products property of object of class SubCategory are not initialized properly.

Make following changes to your code.

Initialize SubCategories properties of obj as following.

Category obj = new Category();
obj.SubCategories = new List<SubCategory>(); // Add this new line to your code

Change following line

if (loop1 == "y" || loop == "Y")

to

if (loop1 == "y" || loop1 == "Y")

Initialize Products property of Subcategory as following.

SubCategory sub = new SubCategory();
sub.Products = new List<Product>(); //Add this new line to your code.

Remove following lines from the code.

List<SubCategory> subcategories = new List<SubCategory>();
List<Product> products = new List<Product>();
Product product1 = new Product();
sub.Products.Add(pro)

The final version of your code should look as following.

Category obj = new Category();
obj.SubCategories = new List<SubCategory>();
int categoryId = 0;
obj.CategoryId = categoryId++;
Console.Write("Enter Category Name : ");
obj.CategoryName = Console.ReadLine();

do
{
    Console.WriteLine("want to add SubCategory ----(Y/N)");
    string loop = Console.ReadLine();
    if (loop == "Y" || loop == "y")
    {
        SubCategory sub = new SubCategory();
        sub.Products = new List<Product>();
        sub.CategoryId = obj.CategoryId;
        sub.SubCategoryId = subcategoryId++;
        Console.Write("EnterSub Category Name : ");
        sub.SubCategoryName = Console.ReadLine();
        do
        {
            Console.WriteLine("want to add product ----(Y/N)");
            string loop1 = Console.ReadLine();
            if(loop1 == "y" || loop1 == "Y")
            {
                Product product = new Product();
                Console.Write("Name :");
                product.ProductId = productId++;
                product.ProdictName = Console.ReadLine();
                Console.Write("Price :");
                product.ProductPrice = Convert.ToDouble(Console.ReadLine());
                product.SubCategoryId = sub.SubCategoryId;
                sub.Products.Add(product);
            }
            else { break; }

        } while (true);
        obj.SubCategories.Add(sub);
    }
    else { break; }
} while (true);

categories.Add(obj);

This will resolve all your issues.

Upvotes: 1

GBursali
GBursali

Reputation: 363

SubCategory sub = new SubCategory(){Products = new List<Product>()};

instead of

SubCategory sub = new SubCategory();

And

Category obj = new Category(){SubCategories = new List<SubCategory>()};

instead of

Category obj = new Category();

And Delete these

 List<Product> products = new List<Product>();
 List<SubCategory> subcategories = new List<SubCategory>();

You should define the subcategories and products, not an different variable, on your class's variables. Nullexception thrown because it couldn't see any list in your obj class, (you have them but not in you class).

Upvotes: 1

Related Questions