locked
Need logic for 1 to many and many to 1? RRS feed

  • Question

  • User-851246726 posted

    I have categories and each category has 5 max of sub-categories.
    So, when I create a new category, I want it to be in constructor.

    What's the best way to create the logic for code?

    I need ideas on how to deal with this in best possible manner.

    Tuesday, April 19, 2016 8:27 PM

Answers

  • User1559292362 posted

    Hi jeffcarter,

    jeffcarter

    So, when I create a new category, I want it to be in constructor.

    What's the best way to create the logic for code?

    According to your description, each category has 5 max of sub-categories, do you mean that if a category has 5 sub-categories, which couldn’t add a new category? And you want to create a new category in constructor? If so, you need to know the parent category before you create a new category, then you could create an object named category, which has a constructor with two parameters like this:

    public class Categroy
        {
            public Categroy() { }
    
            public Categroy(int parentId, string categroyName)
            {
                int count = SubCount(parentId);
                if (count < 5)
                {
                   //create a new Categroy here
                    Console.WriteLine("{0} -- {1}", categroyName, count);
                }
                else
                {
                    throw new Exception("error message");
                }
            }
    
            public int CategroyID { get; set; }
            public string CategroyName { get; set; }
            public int SubCount(int parentId)
            {
                // you need retrieve the count by using parentId
                return 5;
            }
        }

     

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 20, 2016 8:35 AM

All replies

  • User527778624 posted

    Hi,

    o, when I create a new category, I want it to be in constructor.

    what you mean by that, anyway check below code:

    public class Category
    {
       public string Name{get; set;}
       public List<SubCategory> SubCategories{get; set;}
    }
    public class SubCategory
    {
       public string Name{get; set;}
    }

    Wednesday, April 20, 2016 6:45 AM
  • User1559292362 posted

    Hi jeffcarter,

    jeffcarter

    So, when I create a new category, I want it to be in constructor.

    What's the best way to create the logic for code?

    According to your description, each category has 5 max of sub-categories, do you mean that if a category has 5 sub-categories, which couldn’t add a new category? And you want to create a new category in constructor? If so, you need to know the parent category before you create a new category, then you could create an object named category, which has a constructor with two parameters like this:

    public class Categroy
        {
            public Categroy() { }
    
            public Categroy(int parentId, string categroyName)
            {
                int count = SubCount(parentId);
                if (count < 5)
                {
                   //create a new Categroy here
                    Console.WriteLine("{0} -- {1}", categroyName, count);
                }
                else
                {
                    throw new Exception("error message");
                }
            }
    
            public int CategroyID { get; set; }
            public string CategroyName { get; set; }
            public int SubCount(int parentId)
            {
                // you need retrieve the count by using parentId
                return 5;
            }
        }

     

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 20, 2016 8:35 AM