2

I have this:

public abstract class DRT_Tvw_Abstract : TreeView
{
    TableCountMetrics metrics;
    public TableCountMetrics Metrics { get => metrics; set => metrics = value; }

    public class TableCountMetrics
    {
        int schemaNameCount = 0;
        int tableNameCount = 0;

        public int SchemaNameCount { get => schemaNameCount; set => schemaNameCount = value; }
        public int TableNameCount { get => tableNameCount; set => tableNameCount = value; }
    }

    public DRT_Tvw_Abstract() : base()
    {
    }
}

public class DRT_Tvw_TableList_Unfiltered : DRT_Tvw_Abstract
{
    public DRT_Tvw_TableList_Unfiltered() : base()
    {
    }

    public void _CreateTreeView(DataTable dataTable)
    {
        tableListTreeViewNode = new {treeview node from my class that derives from TreeNode}

        Nodes.Add(tableListTreeViewNode);       
}

What I want to do is override the Add method so I can increment/decrement custom integer properties that are part of my DRT_Tvw_Abstract class at the same time I Add a TreeNode. I started by trying to derive from the TreeNodeCollection class, but that seems to be a dead-end because I can't figure out what a valid ctor is for the TreeNodeCollection class

Nodes (as in Nodes.Add) is a property. How do I derive from it so I can override the Add method? I started by creating an override for this poprperty, but I have no clue as to how you override a method (Add) from within property override code.

public new virtual TreeNodeCollection Nodes
{

    //override the Add method from within here somehow?

    get
    {
        return base.Nodes;
    }

    set
    {
    }
}

What is the syntax for overriding the TreeNodeCollection class Add(TreeNode) method?

VA systems engineer
  • 2,856
  • 2
  • 14
  • 38

1 Answers1

4

Add method belongs to TreeNodeCollection class. If you like to have new Add methods with new signatures:

  • Naturally you may think about deriving from TreeNodeCollection and defining new methods and derive from TreeView and replace its TreeNodeCollection with your new custom node collection. But don't try this solution. TreeNodeCollection doesn't have a public constructor. So I'd suggest creating Extension Methods.

  • The other solution that you may think about it is deriving from TreeView and adding new Add methods to the derived TreeView.

Example - Extension Methods

Create a new code file and copy the following code and paste it in the code file:

namespace MyExtensions
{
    using System.Windows.Forms;
    public static class TreeNodeCollectionExtensions
    {
        //Just an example of a new signature, use the signature that you need instead:
        public static TreeNode Add(this TreeNodeCollection nodes, string text, int tag)
        {
            var node = nodes.Add(text);
            node.Tag = tag;
            return node;
        }
    }
}

Then in the class which you are going to use the new Add method, first add the using statement:

using MyExtensions;

Then use it like a normal method:

treeView1.Nodes.Add("One", 1);
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thank you, but I'm not sure how to integrate your code into my code. Against the backdrop of my existing classes `DRT_Tvw_Abstract` and `DRT_Tvw_TableList_Unfiltered`, where does you code go so that I can access my `Metrics` fields? (see updates to my code in the question). – VA systems engineer Jun 28 '18 at 19:01
  • Edited the answer to show you hot to use extension method. – Reza Aghaei Jun 28 '18 at 19:20
  • In my case, the `string text` argument needs to be a class of mine derived from a `TreeNode`. When I modify `public static TreeNode Add(this TreeNodeCollection nodes, string text, int tag)` to `public static TreeNode Add(this TreeNodeCollection nodes, MyTreeNodeClass myTreeNodeClass, int tag)` the call `treeView1.Nodes.Add(myTreeNodeClass, 1);` won't compile: `Error CS7036 There is no argument given that corresponds to the required formal parameter 'myTreeNodeClass' of 'TreeNodeCollectionExtensions.Add(TreeNodeCollection, TreeNode)'`. I'm reading the `Extension Methods` link to figure it out – VA systems engineer Jun 30 '18 at 11:03
  • Starting to wonder about `Extensions`. From [Extension Methods](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods): `In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible, client code that must extend an existing type should do so by creating a new type derived from the existing type. When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break` – VA systems engineer Jun 30 '18 at 12:13
  • About the compiler error, it seems it's ambiguous between your method and an existing method of the `TreeNodeCollection`, you can solve it by using named parameters, or using a different method name. – Reza Aghaei Jun 30 '18 at 12:24
  • About extension methods. What I can say is: It will be one of the best things that you can learn as a C# developer. Apart from extension methods, keep the first bullet point in mind, `TreeNodeCollection` doesn't have a public constructor, so you cannot drive from it. Anyway, you are the one which should choose the solution. – Reza Aghaei Jun 30 '18 at 12:27
  • I guess that's the point: I recognize that the `TreeNodeCollection` ctor is sealed, but that's irrelevant because I'm not trying to derive a class from it. What I'm trying to do is override `Nodes.Add(X)` where `Nodes` is a property of `TreeView` and `X` is a class of mine derived from the `TreeNode` class. I'm trying to figure out how to override the `Add` method which is a virtual method. I should be able to override it – VA systems engineer Jun 30 '18 at 12:42
  • It's relevant, in fact the main important point in the answer. `Nodes` is property of `TreeView`. `Nodes` is of type `TreeNodeCollection`. `Add` is the method of `TreeNodeCollection`. You can not override `Add` without deriving from `TreeNodeCollection`. Read the first bullet point carefully. – Reza Aghaei Jun 30 '18 at 12:51
  • ok, abandoned the `Add` method override approach and went back to an extension. Tried using a different method name, same error CS7036. I give up :-( – VA systems engineer Jun 30 '18 at 14:16
  • Without seeing your code we cannot help. If you need to share the code, do it in a new question, just refer to this question. Make sure you include [MCVE]. – Reza Aghaei Jun 30 '18 at 14:22
  • Please see [Why can't the compiler figure out what the 2nd formal parameter is for my TreeNodeCollection extension method?](https://stackoverflow.com/questions/51117529/why-cant-the-compiler-figure-out-what-the-2nd-formal-parameter-is-for-my-treeno) – VA systems engineer Jun 30 '18 at 18:31
  • I read the new question. You are using the extension method wrong. I believe here I've provided a completely clear example. Follow the example here. – Reza Aghaei Jun 30 '18 at 18:38
  • Sorry - yes - agreed – VA systems engineer Jun 30 '18 at 22:41
  • For other readers: Short version is: you can't override the TreeNodeCollection Add method. Reza's answer to [Why can't the compiler figure out what the 2nd formal parameter is for my TreeNodeCollection extension method?](https://stackoverflow.com/questions/51117529/why-cant-the-compiler-figure-out-what-the-2nd-formal-parameter-is-for-my-treeno) shows how to use an extension method so you can get your hooks into the TreeNodeCollection Nodes.Add method – VA systems engineer Jun 30 '18 at 22:44