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?

Answers


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

Need Your Help

getView of Child Fragment returns null

java android android-listview android-fragments onitemclicklistener

I am creating and replacing a fragment inside another fragments onViewCreated

Java: What causes the performance increase when repeatedly calling a function?

java performance garbage-collection instrumentation jvm-hotspot

I was doing some performance testing regarding object allocation, when I came across a weird result. I have the following java code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.