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 935183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:07:04+00:00 2026-05-15T21:07:04+00:00

I have defined a C#-class, that shall be the elements of a directed graph

  • 0

I have defined a C#-class, that shall be the elements of a directed graph (basically a tree, but an element can have multiple parents – I do not know if there is a special name for that).

Each element shall now all its children and all its parents. It exposes those lists as IEnumarable

public interface IMyClass
{
  public IEnumerable<MyClass> Children { get; }
  public IEnumerable<MyClass> Parents { get; }
}

public class MyClass : IMyClass
{
  private List<MyClass> _parents;
  private List<MyClass> _children;

  public IEnumerable<MyClass> Children
  {
    get { foreach (var child in _children) yield return child; }
  }

  public IEnumerable<MyClass> Parents
  {
    get { foreach (var parent in _parents) yield return parent; }
  }

When adding or removing a child to a given element, I want to make sure, that the given element is added or removed to the parents list of the child too.

First idea was to only expose an AddChild(MyClass theChild) method. Problem is, that I can’t add the parent to the theChild object because it doesn’t expose an AddParent(parent) method and I can’t call private methods of the theChild-object.

So I tried to go with exposing an AddParent(MyClass theParent) method too. To still make sure that both objects links are set, my first shot was calling the AddParent/AddChild in the other function like this:

public void AddChild(IMyClass theChild)
{
  _children.Add(theChild);
  theChild.AddParent(this);
}

public void AddParent(IMyClass theParent)
{
  _parent.Add(theParent);
  theParent.AddChild(this);
}

but obviously, that is a deadly loop.

To make things worse, I want to allow that an element can be the child of another element multiple times (this isn’t a requirement yet, but I want to make sure, that WHEN this requirement comes, my code need not to be touched.)

Are there any algorithms / standard approaches that allow me to make sure, that when adding a child to the parent always both object links are set?

Thanks in advance,
Frank

Edith: Added the interfaces to correct the example.

  • 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-15T21:07:04+00:00Added an answer on May 15, 2026 at 9:07 pm

    You could do this:

    public void AddChild(MyClass theChild)
    {
        _children.Add(theChild);
        theChild._parent.Add(this);
    }
    
    public void AddParent(MyClass theParent)
    {
        _parent.Add(theParent);
        theParent._children.Add(this);
    }
    

    There should be no problems with that; just because you’re referring to a different instance of the class you’re writing the method for doesn’t mean you can’t access it’s members (even if they are private).


    OK with the revised code, you can add two new members to your interface: NotifyChildAdded and NotifyParentAdded, and implement like this:

    public void AddChild(MyClass theChild)
    {
        _children.Add(theChild);
        theChild.NotifyParentAdded(this);
    }
    
    public void AddParent(MyClass theParent)
    {
        _parent.Add(theParent);
        theParent.NotifyChildAdded(this);
    }
    
    public void NotifyChildAdded(MyClass theChild)
    {
        _children.Add(theChild);
    }
    
    public void NotifyParentAdded(MyClass theParent)
    {
        _parent.Add(theParent);
    }
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have defined a CSS class that is being applied to various
I have the following members defined in a class that I'm trying to deserialise:
I have a few tables that I've defined like the below examples: class TableA
I have designed a class which is basically nothing but an object which stores
I have an actionscript file that defines a class that I would like to
I have a class that defines the names of various session attributes, e.g. class
I have a class that defines a CallRate type. I need to add the
I have a class that defines a read-only property that effectively exposes a private
I have a python module that defines a number of classes: class A(object): def
I have defined an interface in C++, i.e. a class containing only pure virtual

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.