开发者

How can I use IList with a master/detail type class?

I have tried:

public class TextFillerParent
    {

   /////// I am not sure if the code on the next 7 lines is correct ///////
        public IList<TextFillerDetail> TextFillerDetails
        {
            get { return _textfillerDetails; }
            set { _textfillerDetails = new List<TextFiller>(value); }
        }

        private List<TextFiller> _textfillerDetails;
    }

    public class TextFillerDetail
    {

        public TextFillerDetail()
        {
            this.Text = new HtmlText();
            this.ImageFile = String.Empty;
        }

        public HtmlText Text { get; set; }
        public string ImageFile { get; set; }
    }

    public class HtmlText
    {

        public HtmlText()
        {
            TextWithHtml = String.Empty;
        }
        [AllowHtml]
        public string TextWithHtml { get; set; }
    }

and then:

        var txt = new TextFillerParent();

  txt.TextFillerDetail开发者_StackOverflow中文版s.Add(new TextFillerDetail()); <<<<<<<<<<<<<<<<<<<<<<
  txt.TextFillerDetails.Add(new TextFillerDetail
  {
      Text = new HtmlText { TextWithHtml = "One" },
      Explanation = new HtmlText { TextWithHtml = "One" }
  });
  txt.TextFillerDetails.Add(new TextFillerDetail
  {
      Text = new HtmlText { TextWithHtml = "Two" },
      Explanation = new HtmlText { TextWithHtml = "Two" }
  });

  var abb = txt.TextFillerDetails[1];

I think something is missing or I am doing something very wrong. when I run the code I get an error Message=Object reference not set to an instance of an object. On the line with <<<<


The way you have it, you've never set TextFillerDetails to a new list. You should only create the new list for _textfillerDetails once:

public IList<TextFillerDetail> TextFillerDetails        
{            
    get { return _textfillerDetails; }
}        

private List<TextFiller> _textfillerDetails = new List<TextFiller>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜