Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4002358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:01:03+00:00 2026-05-20T08:01:03+00:00

I have seen quite a few articles on here about my question but none

  • 0

I have seen quite a few articles on here about my question but none really answer what I am asking. I am creating a class of my Branch objects that you can envision as just like the TreeNode objects of the TreeView control. Each Branch can have any number of Branch children below (and therefore above) it. Here is my rather simple class:

public class Branch {
    public string Name { get; set; }
    public string Link { get; set; }
    public Branch Parent { get; private set; }
    public List<Branch> Children { get; set; }

    internal Branch(string Name, string Link) {
        this.Name = Name;
        this.Link = Link;
        this.Children = new List<Branch>();
    } // Branch - Constructor - Overload

    internal Branch(string Name, string Link, List<Branch> Children) {
        this.Name = Name;
        this.Link = Link;
        this.Children = Children;

        this.Children.ForEach(delegate(Branch branch) {
            branch.Parent = this;
        });
    } // Branch - Constructor - Overload

    public bool HasChildren {
        get { return this.Children.Count > 0; }
    } // HasChildren - Property - ReadOnly

    public string Path {
        get {
            string Result = "";

            Branch parent = this;
            while (parent != null) {
                Result = string.Format("{0}/{1}", parent.Name, Result);
                parent = parent.Parent;
            } // while stepping up the tree

            return string.IsNullOrWhiteSpace(Result) ? "" : Result.Substring(0, Result.Length - 1);
        } // get
    } // Path - Property - ReadOnly

This works GREAT if I Add children at the time of instantiation like the following:

List<Branch> Branches = new List<Branch>() {
    new Branch("First", "#"),
    new Branch("Second", "#"),
    new Branch("Third", "#", new List<Branch>() {
        new Branch("ThirdSub1", "#"),
        new Branch("ThirdSub2", "#")
    }),
    new Branch("Fourth", "#"),
    new Branch("Fifth", "#"),
    new Branch("Sixth", "#", new List<Branch>() {
        new Branch("SixthSub1", "#"),
        new Branch("SixthSub2", "#", new List<Branch>() {
            new Branch("SixthSub2Sub1", "#"),
            new Branch("SixthSub2Sub2", "#"),
            new Branch("SixthSub2Sub3", "#", new List<Branch>() {
                new Branch("Deep Deep Deep Undercover", "#"),
            }),
        }),
    }),
    new Branch("Seventh", "#"),
    new Branch("Eighth", "#"),
};

But if I do the following:

List<Branch> Branches = new List<Branch>();
Branch Test = Branches.Add(new Branch("Something", ""));
Test.Children.Add(new Branch("Child Here", ""));

The “Child Here” node does NOT have a Parent associated with it. Thus it is broken and of course the Path property doesn’t work property.

I thought I could override the List’s Add method but that is not allowed. What is the best way to handle this? Currently I am not creating my own Collection Class like MyBranches, which I like, but if there is a way of doing what I need while implementing IList or ISet or Collection, then I am willing to do so. But please provide an example.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:01:04+00:00Added an answer on May 20, 2026 at 8:01 am

    Just for people in the future looking for this same solution, here is the full class:

    public class Branch {
        public string Name { get; set; }
        public string Link { get; set; }
        public Branch Parent { get; set; }
        public TreeBranches Children { get; private set; }
    
        internal Branch(string Name, string Link) {
            this.Name = Name;
            this.Link = Link;
            this.Children = new TreeBranches(this);
        } // Branch - Constructor - Overload
    
        internal Branch(string Name, string Link, TreeBranches Children) {
            this.Name = Name;
            this.Link = Link;
            this.Children = Children;
    
            this.Children.ToList().ForEach(delegate(Branch branch) {
                branch.Parent = this;
            });
        } // Branch - Constructor - Overload
    
        /// <summary>
        /// Returns a boolean indicating if the given Branch has any child Branches.
        /// </summary>
        public bool HasChildren {
            get { return this.Children.Count > 0; }
        } // HasChildren - Property - ReadOnly
    
        /// <summary>
        /// Gets the path from the oldest ancestor to the current Branch.
        /// </summary>
        public string Path {
            get {
                string Result = "";
    
                Branch parent = this;
                while (parent != null) {
                    Result = string.Format("{0}/{1}", parent.Name, Result);
                    parent = parent.Parent;
                } // while stepping up the tree
    
                return string.IsNullOrWhiteSpace(Result) ? "" : Result.Substring(0, Result.Length - 1);
            } // get
        } // Path - Property - ReadOnly
    
    } // Branch - Class
    
    public class TreeBranches : IList<Branch> {
        private List<Branch> branches = new List<Branch>();
        private Branch owner;
    
        public TreeBranches() {
            this.owner = null;
        }
    
        public TreeBranches(Branch owner) {
            this.owner = owner;
        }
    
        public void Add(Branch branch) {
            branch.Parent = this.owner;
            this.branches.Add(branch);
        }
    
        #region Standard IList Method Implementation
    
        IEnumerator<Branch> IEnumerable<Branch>.GetEnumerator() { return this.branches.GetEnumerator(); }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.branches.GetEnumerator(); }
        public int IndexOf(Branch item) { return this.branches.IndexOf(item); }
        public void Insert(int index, Branch item) { this.branches.Insert(index, item); }
        public void RemoveAt(int index) { this.branches.RemoveAt(index); }
        public Branch this[int index] {
            get { return this.branches[index]; }
            set { this.branches[index] = value; }
        }
    
        public void Clear() { this.branches.Clear(); }
        public bool Contains(Branch item) { return this.branches.Contains(item); }
        public void CopyTo(Branch[] array, int arrayIndex) { this.branches.CopyTo(array, arrayIndex); }
        public int Count { get { return this.branches.Count(); } }
        public bool IsReadOnly { get { return this.IsReadOnly; } }
        public bool Remove(Branch item) { return this.branches.Remove(item); }
    
        #endregion Standard IList Method Implementation
    } // TreeBranches - Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen the reverse of this question quite a few times, but have
I have seen quite a few questions here about the best storage ideologies for
I have seen quite a few questions about this but cant seem to find
I really was trying to avoid asking this question. I have seen quite a
I've seen quite a few threads about ClassNotFoundException and Android, but I have yet
I have seen in quite few places (one example here: http://pascalgamedevelopment.com/archive/index.php/t-1204.html ) people doing
I have seen quite a few ways to output variables. Which of these ways
I have seen questions posted on here asking how to left and right align
I have seen this talked about but never answered. Maybe it has and I'm
I have seen quite a few code samples/plugins that promote uploading assets directly to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.