2016-10-03 2 views
0

私は困惑しています。私はそうのように見えるコンストラクタクラスを持っている:コンストラクタクラスに追加

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; 
     } 
    }; 
} 

私はそうのようなクラスのコンストラクタ部分に追加する:

  //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")) 
     // }, 

私の問題は、私はこのクラスに追加しようとしているありますループを介して。だから私は簡単にCCategoryGroupを追加することができます:

cCategory.Add(new CCategoryGroup(name, value) 

以前に示したようにCCategoryコンストラクタにどのように追加しますか?

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); 
       } 
      }); 
     } 

私はXMLを解析し、その結果をクラスに追加しようとしています。これは明らかに機能しません。しかし、私がしようとしているもののサンプルです。最初の ".add" to cCategoryGroupはうまく動作します。コンストラクタのCCategoryコメントアウトされたコードでやった方法を追加することはできません。

+0

コンパイルするのは、欠落している4番目のパラメータを 'CCategory'コンストラクタ呼び出しに追加することです。あなたは何か違うものを探しているのですか、それともあなたのために働かないのですか? –

+3

また、すべてを 'C'で始めるのを止めてください。クラスとパラメータの名前を読みにくくし、強く型付けされた言語では不要にします。 –

+0

はい、私はループを通してそれを実行するために探しています。私は、コメントアウトされたコードの作品を​​知っているが、私は基本的にクラスとコンストラクタに ".add"を使用して追加しようとしています。 ".add"を使ってコンストラクタにどのように追加するのですか? –

答えて

0

いいえ、あなたがそのようなコレクション初期化子内ループを使用することはできませんが、初期化せずにそれを行うことができます。初期化子がちょうどによってAdd一連の呼び出しに変換されることを

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)); 
    } 
} 

注意コンパイラは、機能的にはそれとループの追加の間に違いはありません。

+0

本当にありがとうございました! –

関連する問題