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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:31:49+00:00 2026-05-15T09:31:49+00:00

Lets say I have the following code: public class Base { // Some stuff

  • 0

Lets say I have the following code:

public class Base
{
    // Some stuff here
    public int var
}

public class UsingBase
{
    // Some more stuff here
    public Base B = new Base();
}

Now let’s say the actual code of these classes is huge and I would like UsingBase to do some extra stuff. So I’m thinking about subclassing it. Now let’s say that what I really want to do is add a method that gives me two times var. Since var is a field of Base, it would be more elegant to add the method to a subclass of Base, and not to a subclass of UsingBase. But I don’t want to rewrite UsingBase, and UsingBase uses Base and not my subclass of Base. Actually, this is exactly what I want to do:

TreeView (.NET) has a property of type TreeNodeCollection called Nodes that stores a bunch of objects of type TreeNode. Each TreeNode in Nodes can have a Tag that I can use to store any object I want. But the class TreeNodeCollection doesn’t have a function that gives me the TreeNode that correspond to a certain Tag. So I want to be able to do this:

TreeView myTreeView = new TreeView();
// Add some TreeNode's and asign unique tags to each of them
TreeNode someNode = myTreeView.Nodes.GetNodeByTag(object Tag);

I know I can subclass TreeView and add a function that does that, but I just like the other solution a lot better. So I’m wondering if that’s possible.

  • 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-15T09:31:50+00:00Added an answer on May 15, 2026 at 9:31 am

    Since TreeNodeCollection implements IEnumerable, you can just use a LINQ where clause to find nodes by particular tags.

    i.e. :

    TreeNode myTaggedNode =  myTreeView.Nodes.Cast<TreeNode>().Where(n=>n.Tag == myTag).SingleOrDefault()
    

    I’d personally avoid subclassing in this context.

    EDIT: Based on your comment; you’re wondering whether you can get your treeview to use your subclassed version of TreeNodeCollection that contains the GetNodeByTag(object tag) method. There’s two ways to do this (both of which are going to be overkill solutions compared to the one I presented originally)

    Method 1) Subclass TreeNodeCollection and include your GetNodeByTag(object tag) method. Then subclass TreeView to use your derived TreeNodeCollection

    This won’t work, since the Nodes property on TreeView isn’t virtual

    Method 2) Use an extension method on TreeNodeCollection that wraps the linq expression I showed you originally:

    public static TreeNodeCollectionExtensions
    {
        public static TreeNode GetNodeByTag(this TreeNodeCollection tnc, object tag)
        {
            return tnc.Cast<TreeNode>().Where(n=>n.Tag == tag).SingleOrDefault();        
        }
    
    }
    

    But again, this is simply wrapping the original linq expression I provided originally.

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

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As jAndy states the problem is event bubbling here, but… May 16, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer Is the Postgresql server running on Windows as well as… May 16, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer Honestly, what ever else, besides the Concrete Factory classes, do… May 16, 2026 at 7:03 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.