phil
phil

Reputation: 390

Access to members of a class from an item inside a list in that class

class Section
{
    public string       Name;
    public List<Option> Options;

    public class Option
    {
        public string Name;
        public string Value;
        public string Path
        {
            get { return "SectionName.OptionName=Value"; }
        }
    }
}

In other words, I would like to have access to members of Section from Option, which will always be used as a list. What is an elegant way to implement this?

Upvotes: 1

Views: 78

Answers (1)

user27414
user27414

Reputation:

An Option needs a reference to its Section:

class Section
{
    public string       Name;
    public List<Option> Options;

    public class Option
    {
        public Section MySection { get; private set; }
        public string Name;
        public string Value;
        public string Path
        {
            get { return string.Format("{0}.{1}={2}", this.MySection.Name, this.Name, this.Value); }
        }

        public Option(Section mySection)
        {
            this.MySection = mySection;
        }
    }
}

Upvotes: 4

Related Questions