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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:32:23+00:00 2026-05-27T10:32:23+00:00

I am using a recursive function for a object tree. That is, my object

  • 0

I am using a recursive function for a object tree. That is, my object collection is like:

Object1
--Object2
----Object3
------Object4

All objects inherits from a base object (abstract class), where has a Validate() method, and its collection inherits from ITreeCollection. I have written a recursive function to perform that:

private bool Validate<T>(ITreeCollection items) where T : TreeNodeBase
        {
            foreach (var itm in items as Collection<T>)
            {
                if (((TreeNodeBase)itm).Items != null)
                {
                    return Validate<T>(((TreeNodeBase)itm).Items);
                }
                else return true;
            }
            return true;
        }

How can I derive the the type parameter T for the inner function (i.e. return Validate<T>(((TreeNodeBase)itm).Items))

  • 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-27T10:32:24+00:00Added an answer on May 27, 2026 at 10:32 am

    Firstly, as it stands, you are not using the type parameter T so it could safely be removed. However I imagine you might want to do some type specific validation so this is perhaps not that helpful a suggestion. But, without example of what you want to do with T it’s difficult to make suggestions.

    Anyhow, here’s one approach of what I think you are trying to do:

    private bool Validate(ITreeCollection items)
    {
        foreach (TreeNodeBase node in (IEnumerable) items)
        {
            // validate the node itself first
            if (!Validate(node))
            {
                return false;
            }
    
            if (node.Items != null)
            {
                // validate its children
                if (!Validate(node.Items)
                {
                    return false;
                }
            }
        }
    
        return true;
    }
    
    private bool Validate(TreeNodeBase node)
    {
        if (node is BananaNode)
        {
            var bananaNode = (BananaNode) node;
            //TODO do BananaNode specific validation
        }
        else if (node is AppleNode)
        {
            var appleNode = (AppleNode) node;
            //TODO do AppleNode specific validation
        }
        else
        {
            throw new ArgumentOutOfRangeException("Cannot validate node of type '" + node.GetType().Name + "'.");
        }
    }
    

    You could get snazzy with the dynamic keyword and get rid of some of this type checking but it does get a bit confusing and I’d advise against it:

    private bool Validate(ITreeCollection items)
    {
        foreach (TreeNodeBase node in (IEnumerable) items)
        {
            // validate the node itself first
            if (!Validate((dynamic) node)) // this will call the most appropriate version of Validate
            {
                return false;
            }
    
            if (node.Items != null)
            {
                // validate its children
                if (!Validate(node.Items)
                {
                    return false;
                }
            }
        }
    
        return true;
    }
    
    private bool Validate(BananaNode node)
    {
        //TODO do BananaNode specific validation
    }
    
    private bool Validate(AppleNode node)
    {
        //TODO do AppleNode specific validation
    }
    
    private bool Validate(TreeNodeBase node)
    {
        throw new ArgumentOutOfRangeException("Cannot validate node of type '" + node.GetType().Name + "'.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a recursive tree of hashmaps, specifically Hashmap map where Object is a
I have a javascript object that I would like to update with data from
I have a member function of an object that is typically used in an
I'm using a small tree/graph package ( django_dag ) that has that give my
I am using a recursive function based around for(.. in ..) and hasOwnProperty to
I'm trying to write a recursive function that does some formatting within a file
I've tried using the function like this: AUC(WM,time=Grand.trial,id=Feed,dv=Distance.moved) but it results in this error
Consider the following jQuery implementation defined using an object literal... $(function() { var myObject
In the code below I am using a recursive CTE(Common Table Expression) in SQL
Using recursion, find an index that cuts an array in two parts so that

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.