0

I'm stumped. I have a constructor class that looks like so:

    class CClasses
{
    public class CCategoryGroup : List<CCategory>
    {
        public string CTitle { set; get; }
        public string CShortTitle { set; get; }
        public CARESCategoryGroup(string ctitle, string cshorttitle)
        {
            this.CTitle = ctitle;
            this.CShortTitle = cshorttitle;
        }
    };

    public class CCategory
    {
        public int CID { set; get; }
        public string CName { set; get; }
        public ImageSource CIcon { set; get; }
        public string CUrl { set; get; }
        public CCategory(int cid, string cname, ImageSource cicon, string curl)
        {
            this.CID = cid;
            this.CName = cname;
            this.CIcon = cicon;
            this.CUrl = curl;
        }
    };
}

I want to add to the constructor portion of the class like so:

            //List<CCategoryGroup> ccategory = new List<CCategoryGroup>
        //{
        //    new CCategoryGroup("Dolphin", "Dolphin Group")
        //    {
        //        new CCategory(1, "Bear", ImageSource.FromFile("bear.png")),
        //        new CCategory(2, "Elephant", ImageSource.FromFile("elephant.png")),
        //        new CCategory(3, "Dog", ImageSource.FromFile("dog.png")),
        //        new CCategory(4, "Cat", ImageSource.FromFile("cat.png")),
        //        new CCategory(5, "Squirrel", ImageSource.FromFile("squirrel.png"))
        //    },

My problem is I'm trying to add to this class through a loop. So I'm easily able to add the CCategoryGroup with:

cCategory.Add(new CCategoryGroup(name, value)

How do I add to the CCategory constructor as shown previously?

foreach (XElement catelement in xmlDoc.Descendants(xmlNS + "Category"))
        {

            cCategory.Add(new CCategoryGroup(catelement.Element(xmlNS + "Name").Value, catelement.Element(xmlNS + "Name").Value){
                foreach (XElement subcatelement in xmlDoc.Descendants(xmlNS + "SubCategory"))
                {
                    i++;

                    new CCategory(i, subcatelement.Element(xmlNS + "Name").Value, "", subcatelement.Element(xmlNS + "URL").Value);
                }
            });
        }

I'm parsing XML and trying to add the results to the class. This does not work, obviously. But is a sample of what I'm trying to do. The first ".add" to cCategoryGroup works great, its the constructor CCategory I cannot add too the way I did in the commented out code.

  • What you have would compile is you add the missing 4th parameter to the `CCategory` constructor calls. Are you looking for something different or does it not work for you? – D Stanley Oct 03 '16 at 20:28
  • 3
    Also stop prefacing everything with `C`. It makes the class and parameter names harder to read and is not necessary in a strongly-typed language. – D Stanley Oct 03 '16 at 20:29
  • Yes, I'm looking to run it through a loop. I know the commented out code works, but I'm basically trying to add to a class and the constructor using ".add". How do I add to the constructor using ".add"? – Andy Jackson Oct 03 '16 at 20:31
  • Then I'm not clear on what you're _trying_ to do. Can you post something complete that _doesn't_ work so we can understand how to fix it? – D Stanley Oct 03 '16 at 20:33
  • Also is seems like you're trying to add a `CategoryGroup` to a `Category` which is backwards. Are you trying to add a new category to an existing group? – D Stanley Oct 03 '16 at 20:34
  • I added what doesn't work – Andy Jackson Oct 03 '16 at 20:40
  • OK I see what you're trying now. No you can't have a loop within an initializer, but you can just use `Add` within the loop. – D Stanley Oct 03 '16 at 20:48
  • Thank you. But how do I reference the base class and add the new constructor to it, so it resembles the commented code? – Andy Jackson Oct 03 '16 at 20:51
  • You may want to give a read through of "[Why not inherit from List?](http://stackoverflow.com/questions/21692193/why-not-inherit-from-listt)" – Scott Chamberlain Oct 03 '16 at 21:35

1 Answers1

0

No, you can't use a loop within the collection initializer like that, but you can do it without an initializer:

foreach (XElement catelement in xmlDoc.Descendants(xmlNS + "Category"))
{

    CCategoryGroup categoryGroup = new CCategoryGroup(catelement.Element(xmlNS + "Name").Value, catelement.Element(xmlNS + "Name").Value;
    cCategory.Add(categoryGroup);

    foreach (XElement subcatelement in xmlDoc.Descendants(xmlNS + "SubCategory"))
    {
        i++;
        categoryGroup.Add(new CCCategory(i, subcatelement.Element(xmlNS + "Name").Value, "", subcatelement.Element(xmlNS + "URL").Value));
    }
}

Note that an initializer just gets translated to a series of Add calls by the compiler, so functionally there's no difference between that and adding in a loop.

D Stanley
  • 149,601
  • 11
  • 178
  • 240