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

  • Home
  • SEARCH
  • 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 5958371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:32:00+00:00 2026-05-22T18:32:00+00:00

The project I’m currently working on requires me to create a tree data structure.

  • 0

The project I’m currently working on requires me to create a tree data structure.
Below is the sample how I’ve tried to achieve this functionality. I’ve decided to create a child node collection as a nested class since it allows me to set Nodes parent in it’s Add() method while keeping parent setter private, so that classes derived from Node or other classes in the same assembly couldn’t get direct access to it.

class Node<T> where T : Node<T>
{
  private T mParent;
  private ChildNodeCollection<T> mChildren;

  public T Parent
  {
    get{return this.InnerParent;}
  }

  private T InnerParent
  {
     get{return this.mParent;}
     set {this.mParent = value;}
  }

  public Node()
  {
      this.mChildren = new ChildNodeCollection<T>(this);
  }

  class ChildNodeCollection<U> where U : T
  {
       private U mParent;

       public U CollectionParent
       {
           get{return this.mParent;}
       }

       public ChildNodeCollection(U parent)
       {
           this.mParent = parent;
       }


        public void Add(U item)
        {
            item.InnerParent = this.CollectionParent;

            ...
        }

  }
}

This code doesn’t compile though. It complains about this.mChildren = new ChildNodeCollection(this) line in the Node constructor. It throws these two errors.

Error   35  The best overloaded method match for Node<T>.ChildNodeColllection<T>.ChildNodeColllection(T)' has some invalid arguments

Error   36  Argument '1': cannot convert from Node<T> to T

I guess it can’t work out that T is Node, even though I specified so in the class definition. I’m curious if anybody has any ideas how this could be done differently in a way that would allow me to set Node’s parent when adding it to collection without exposing Node’s Parent property too much with internal access modifier.

  • 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-22T18:32:01+00:00Added an answer on May 22, 2026 at 6:32 pm

    In any case, you need to explicitly specify the type argument when creating a generic object using constructor, so you need to write something like:

    this.mChildren = new ChildNodeCollection<T>(this);
    

    This won’t work because the type of this is Node<T> and not T (which is what the constructor requires). I think the easiest way to fix it is to store the parent as Node<T> instead of using a generic parameter.

    The relevant parts of code would look like this:

    public Node() {
      this.mChildren = new ChildNodeCollection(this);
    }
    
    class ChildNodeCollection {
      private Node<T> mParent;
    
      public ChildNodeCollection(Node<T> parent) {
        this.mParent = parent;
      }
    }
    

    I guess that your original goal (with using the T : Node<T> constraing) was to use inheritance to define more specific types of nodes. Then you’d like to retreive children (or parents) statically typed as T (that is, your specific node type). I may be wrong, but I seriously doubt this can be expressed with .NET generics.

    I think it is a lot easier to use Node<T> as a type representing a node that contains your value of type T instead of using inheritance.

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

Sidebar

Related Questions

The project I'm working is using n-tier architecture. Our layers are as follows: Data
Every project invariably needs some type of reporting functionality. From a foreach loop in
A project I'm working on at the moment involves refactoring a C# Com Object
My project is currently using a svn repository which gains several hundred new revisions
The project is ASP.NET 2.0, I have never been able to reproduce this myself,
My project is split up into a typical 3 layer structure for a Silverlight
My project is not running on xcode3.2 with ios SDK3.2 but working fine in
Project Darkstar was the topic of the monthly JavaSIG meeting down at the Google
My project is based on spring framework 2.5.4. And I try to add aspects
My project has both client and server components in the same solution file. I

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.