Click here to Skip to main content
15,888,058 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Grettings All,

Please forgive me if this is a function and not a method. Not sure the difference when I typed this question.

When I look for code online I see code that are in methods instead of button clicks so it's hard for me to use what I find because I do not know how to call them correctly. Usually, I would think to use the
PopulateTreeView();
but it does not work. Can someone show me how to call this method from another place in my program? I will be calling it from load or button click.

All I am looking for is if I find code that I would like to use while researching my issues and if I find something, but uses methods how do you know how to call it and what to use in the parameters?

<pre lang="c#">

private void PopulateTreeView(int parentId, TreeNode parentNode)
{
    String Seqchildc = "SELECT MENUPARVAL,FRM_NAME,MNUSUBMENU FROM MNUSUBMENU WHERE MENUPARVAL=" + parentId + "";
    SqlDataAdapter dachildmnuc = new SqlDataAdapter(Seqchildc, conn);
    DataTable dtchildc = new DataTable();
    dachildmnuc.Fill(dtchildc);
    TreeNode childNode;
    foreach (DataRow dr in dtchildc.Rows)
    {
        if (parentNode == null)
            childNode = treeView1.Nodes.Add(dr["FRM_NAME"].ToString());
        else
            childNode = parentNode.Nodes.Add(dr["FRM_NAME"].ToString()); 
            PopulateTreeView(Convert.ToInt32(dr["MNUSUBMENU"].ToString()), childNode);
    }
}


What I have tried:

Google and trying to figure it out on my own
Posted
Updated 20-Aug-18 18:05pm
v2

Start by looking at the function / method definition - it's the first line of the method (Don't worry about the difference, just call them "methods" in C# and mentally translate "function" to "method" when you see it. They are just different terms for the same thing).

So in the code you show, the first line is:
C#
private void PopulateTreeView(int parentId, TreeNode parentNode)
And that is the method definition, which describes the method and how you use it.
private                  Access control: how visible it it to the outside world?
                         In this case, it is only visible inside the class in which 
                         it is defined.
void                     Return type. In this case, teh methid does not return any value.
PopulateTreeView         Method name
(                        Start of parameter list
int parentId,            First parameter: an integer
TreeNode parentNode      Second parameter: a TreeNode
)                        End of parameter list
This is called the method signature.

Because none of the parameters are optional (i.e. they aren't followed by an equals sign and a default value) you must provide both of them when you call the method, you cannot omit either or you will get a compiler error. (There is a way to call a method with different parameters, but that involves writing a similar method with the same name that takes different parameters: a method with a different signature. This is called method overloading.)

So when you call it, you pass values as the parameters:
C#
PopulateTreeView(666, listHead);
for example.

The exact values you pass are up to you: the ones I show will probably not work at all, but it should be obvious from wherever you got the method source code what exactly to pass to it!
 
Share this answer
 
Thank you for the reply OriginalGriff. I found the solution I believe, however, I am not really sure how this all works. It was on the original site where the code was, as well as, I Googled part of the code and found something very similar here on codeproject. Since they both are very close I will consider that this is correct.

<pre lang="c#">PopulateTreeView(Convert.ToInt32(dr["MNUSUBMENU"].ToString()), childNode); 


Too me, seems like a lot of stuff just to call a method, but as you can tell I am still learning.

Thank you again!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900