The pertinent parts of my code are below. In the MyTreeView
class (last block of code below), the line of code TncExtensions.TncNodeAdd(this, myTreeViewNode);
generates the error CS7036 There is no argument given that corresponds to the required formal parameter 'myTreeViewNode' of 'TncExtensions.TncNodeAdd(TreeNodeCollection, MyTreeView_Abstract, MyTreeViewNode_Abstract)'
Why can't the compiler figure out what the 2nd formal parameter is for my TreeNodeCollection extension method?
public static class TncExtensions
{
public static int TncNodeAdd(this TreeNodeCollection nodes, MyTreeView_Abstract myTreeView, MyTreeViewNode_Abstract myTreeViewNode)
{
return myTreeView.Nodes.Add(myTreeViewNode);
}
}
public abstract class MyTreeViewNode_Abstract : TreeNode
{
public MyTreeViewNode_Abstract(string text) : base(text)
{
}
}
public class MyTreeViewNode : MyTreeViewNode_Abstract
{
public MyTreeViewNode(string text) : base(text)
{
}
}
public abstract class MyTreeView_Abstract : TreeView
{
}
public class MyTreeView : MyTreeView_Abstract
{
public void CreateTree()
{
MyTreeViewNode myTreeViewNode = new MyTreeViewNode("node text");
TncExtensions.TncNodeAdd(this, myTreeViewNode);
}
}
Reza'a answer is still the answer, but to make my treenode
Add
extension method useful, I had to modify my calls to it. I forgot that as part of adding a treenode, you need to specify what node collection you want to append the new node to.Note: See code in original question for the basic classes involved
Modified Add extension method as suggested by Reza
Use it like this
I believe the extension method which you created is not of much use but if you are curious to know what the problem is, you are using the extension method wrong. You are accessing it like a static method, in this style you need to pass 3 arguments based on the signature of the method:
Or use it like an extension method:
I suggest you change the extension method to: