Question: I have the following two methods which loop either a TreeView or TreeNode which I want to combine into a single method. Instead of passing the TreeView/TreeNode I could pass an Object and check the type but wanted to know if there was better way?

```
private TreeNode SearchNode(string nodetext, TreeNode node)
{
    foreach (TreeNode nd in node.Nodes)
    {
        if (nd.Text == nodetext)
        {
            return nd;
        }
    }
    return null;
}

private TreeNode SearchParentNode(string nodetext, TreeView trv)
{
    foreach (TreeNode node in trv.Nodes)
    {
        if (node.Text == nodetext)
        {
            return node;
        }
    }
    return null;
}

```

Answer: 
You want a single method that can search for a value in a `NodesCollection`, so just extract the contents of both methods into a single, third method that accepts a `NodesCollection`:

```
private TreeNode SearchNodes(string nodeText, TreeNodesCollection nodes)
{
    foreach (TreeNode node in nodes)
    {
        if (node.Text == nodeText)
        {
            return node;
        }
    }
    return null;
}

```

And pass it whatever collection you want to search:

```
return SearchNodes(nodetext, node.Nodes);

return SearchNodes(nodetext, trv.Nodes);

```

Also, you can reduce the `SearchNodes` method to a single line using LINQ:

```
private TreeNode SearchNodes(string nodeText, TreeNodesCollection nodes)
{
    return nodes.Cast<TreeNode>().FirstOrDefault(n => n.Text == nodeText);
}

```
```
private TreeNode SearchNode(string nodetext, object obj)
{
    if(obj.GetType() Is TreeView)
    {
          t = (TreeView)obj;
          foreach (TreeView tv in obj.Nodes)
          {
             if (tv.Text == nodetext)
             {
                 return nd;
             }
          }
    }
    else
    {
          n = (TreeNode)obj;
          foreach (TreeNode nd in obj.Nodes)
          {
              if (nd.Text == nodetext)
              {
                 return nd;
              }
          }
    }
    return null;
}

```