What is the correct way to call an extension method (TreeNodeCollection Add method)?


What is the correct way to call an extension method (TreeNodeCollection Add method)?



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)'


MyTreeView


TncExtensions.TncNodeAdd(this, myTreeViewNode);


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);
}
}





You declared an extension method, but you used it like a normal static method. If you call it like a normal static method, all arguments need to be provided (in your case 3, not just 2). That said, it kinda looks wrong trying to call an extension method like a normal static method. Perhaps look into a tutorial about extension methods to clarify how they are usually used/called.
– elgonzo
Jun 30 at 18:36






You are using the extension method wrong.
– Reza Aghaei
Jun 30 at 18:37





Also, what precisely is the purpose of the nodes argument in your extension method? Your extension method is not using nodes, so what is the point of it having it there?
– elgonzo
Jun 30 at 18:43



nodes


nodes





@HansPassant: I have to disagree. Originally, I was using the base TreeNodeCollection Add method to add a custom TreeView node to a custom TreeView. In my custom TreeView, I have custom properties that track the state of my custom TreeView. Without getting hooks into the Add method, I had to update the custom properties using inline code immediately after each call to the base Add method . By using an Add extension method and a custom property that ties the custom node to the custom TreeView, I can update the custom TreeView properties in one central location.
– Nova Sys Eng
Jul 1 at 13:38



TreeNodeCollection Add


TreeView


TreeView


TreeView


TreeView


Add


Add


Add


TreeView


TreeView




2 Answers
2



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:


TncExtensions.TncNodeAdd(Nodes, this, myTreeViewNode);



Or use it like an extension method:


this.Nodes.TncNodeAdd(this, myTreeViewNode);



I suggest you change the extension method to:


public static int TncNodeAdd(this TreeNodeCollection nodes, MyTreeViewNode_Abstract myTreeViewNode)
{
return nodes.Add(myTreeViewNode);
}





Thanks for the guidance on how to use the extension method correctly. I wasn't getting it (obviously). The extension method doesn't look like much because I provided only the minimal code required to ask my question. Now that you've taught me how to call the extension method correctly, I can achieve the goal stated in the original override question: increment/decrement custom integer properties that are part of my MyTreeView class at the same time I Add a MyTreeViewNode to my MyTreeView.
– Nova Sys Eng
Jun 30 at 22:37



increment/decrement custom integer properties that are part of my MyTreeView class at the same time I Add a MyTreeViewNode to my MyTreeView





That's great. You're welcome.
– Reza Aghaei
Jun 30 at 22:38





p.s.: After I modified my calling code to be IAW with your answer, I was able to rename the extension method to simply Add
– Nova Sys Eng
Jul 1 at 0:22



Add





Yes, you can, there is no naming conflict.
– Reza Aghaei
Jul 1 at 5:19





One more update (your answer is still the accepted answer)
– Nova Sys Eng
Jul 1 at 13:05



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.


Add



Note: See code in original question for the basic classes involved



Modified Add extension method as suggested by Reza


namespace TreeNodeCollectionExtensions
{
public static class TncExtensions
{
public static int Add(this TreeNodeCollection nodes, DRT.DRT_TvwNode_Abstract myTreeViewNode)
{
int newNodeIndex = nodes.Add(myTreeViewNode);

//Do stuff to custom properties that are members of DRT_TvwNode_Abstract and classes derived from DRT_TvwNode_Abstract

return newNodeIndex;
}
}
}



Use it like this


using TreeNodeCollectionExtensions;

public class MyTreeView : MyTreeView_Abstract
{
public MyTreeView() : base()
{
}

public void CreateTree()
{
MyTreeViewNode myTreeViewNode;

//Add node to root of TreeView
//Using named parameter to force the compiler to
//use the Add extension method and not the base Add method
myTreeViewNode = new MyTreeViewNode("root node text");
Nodes.Add(myTreeViewNode: myTreeViewNode);

//Add node one level below root node of TreeView
//Using named parameter to force the compiler to
//use the Add extension method and not the base Add method
myTreeViewNode = new MyTreeViewNode("level 1 node text");
int newLevel1NodeIndex = Nodes[0].Nodes.Add(myTreeViewNode: myTreeViewNode);

//Add node one level below level 1 node just created
//Using named parameter to force the compiler to
//use the Add extension method and not the base Add method
myTreeViewNode = new MyTreeViewNode("level 2 node text");
int newLevel2NodeIndex = Nodes[0].Nodes[newLevel1NodeIndex].Nodes.Add(myTreeViewNode: myTreeViewNode);

//etc., etc.

}
}





this TreeNodeCollection nodes this is the node collection which you are going to add to it. What's the point of having the second parameter? When you call treeView1.Nodes.Add() why do you want to have another node collection as parameter? You have the nodes collection in the first parameter of extension method.
– Reza Aghaei
Jul 1 at 13:09



this TreeNodeCollection nodes


treeView1.Nodes.Add()





I'm not sure you have got the usage of extension methods correctly. An extension method is going to add a new method to an instance of object, and always, you define that instance as first parameter of the method. But when calling the extension method, you don't need to pass the object to the method. For example. Let's say you create a Reverse extension method for string: public static string Reverse (this string input), then as the usage, it's enough to call it like this: someString.Reverse() without passing the instance. In fact someString will be passed as input to the method.
– Reza Aghaei
Jul 1 at 13:19


Reverse


public static string Reverse (this string input)


someString.Reverse()


someString


input





You're right - I'm um... struggling to understand this stuff. I started as a developer in the 80's (procedural programming). Started learning OOD//OOP and C# recently. It has been/is a challenge. I fixed my update. I think I have it now
– Nova Sys Eng
Jul 1 at 13:26





Yes, now you have it correctly.
– Reza Aghaei
Jul 1 at 13:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API