Search code examples
c#accessorclass-properties

How to auto populate a property within a List property of a Class


Firstly, thanks in advance for anyone who can help.

I have a header and lines scenario. I want the lines object to auto-populate with the headerId from the Header. Please can someone help?

public class Header
{
    public int headerId { get; set; }
    public List<Lines> lines { get; set; }
}

public class Lines
{
    public int lineId { get; set; }
    public int headerId { get; set; }   //<<< want to autopopulate this from Header object
}

Solution

  • There's a number of ways you can do this.

    Option 1 - Fix on collection set.

    public class Header
    {
        private List<Lines> _lines
        public int HeaderId { get; set; }
        public List<Line> Lines 
        { 
            get { return _lines; }
            set 
            { 
                _lines = value;
                if(_lines != null)
                {
                    foreach(var line in _lines)
                        line.HeaderId = HeaderId;
                }
            }
        }
    }
    
    public class Line
    {
        public int LineId { get; set; }
        public int HeaderId { get; set; }  
    }
    

    The problem with this route is that the HeaderId is only set when you set Lines collection. That means you can change it after the fact or any new items you add to Lines will not be corrected.

    Option 2 - Pass in the Header when constructing the Line

    public class Header
    {
    
        public int HeaderId { get; set; }
        public List<Line> Lines { get; set; }
    }
    
    public class Line
    {
        public Header ParentHeader { get; }
        public int LineId { get; set; }
        public int? HeaderId { get { return ParentHeader?.HeaderId; }  
    
        public Line(Header header)
        {
            ParentHeader = header;
        }
    }
    

    As long as you have the same Header instance for all of the lines, if you were change the Header.HeaderId all of the lines will automatically be updated.

    Option 3 - Don't add an instance of Line directly to the collection

    public class Header
    {
        List<Line> _lines = new List<Line>();
    
        public int HeaderId { get; set; }
        public ReadOnlyCollection<Line> Lines { get {return _lines.AsReadOnly(); }
    
    
        public void AddLine(Line line)
        {               
            line.HeaderId = HeaderId;
            _lines.Add(line);
        }
    }
    

    This will have the same issue as Option 1.