Hi,

When I try to build my code it gives an error "Not all code paths return a value".
Please find the code below:

public TreeNode FindInstanceNode(string SubContainer,string instance)
        {
            TreeNode Node = GetTreeViewClickedNode();
            foreach (TreeNode trNode in Node.Nodes)
            {
                if (trNode.Text == "SUB-CONTAINERS")
                {
                    TreeNodeCollection tcSubNodes = trNode.Nodes;
                    foreach (TreeNode trInode in tcSubNodes)
                    {
                        TreeNodeCollection tcInsNodes = trInode.Nodes;
                        foreach (TreeNode InstanceNode in tcInsNodes)
                        {
                            if (InstanceNode.Text == "INSTANCE" && InstanceNode.FirstNode.Name == instance)
                            {
                                return InstanceNode;
                            }
                        }

                    }

                }

            }

        }

Recommended Answers

All 3 Replies

public TreeNode FindInstanceNode(string SubContainer,string instance)
{
    TreeNode Node = GetTreeViewClickedNode();

    foreach (TreeNode trNode in Node.Nodes)
    {
        if (trNode.Text == "SUB-CONTAINERS")
        {
            TreeNodeCollection tcSubNodes = trNode.Nodes;
            
            foreach (TreeNode trInode in tcSubNodes)
            {
                TreeNodeCollection tcInsNodes = trInode.Nodes;
                
                foreach (TreeNode InstanceNode in tcInsNodes)
                {
                     if (InstanceNode.Text == "INSTANCE" && InstanceNode.FirstNode.Name == instance)
                    {
                        return InstanceNode;
                    }
                }
            }
        }
    }
}

Your code is invalid because it is possible to exit that method without returning a TreeNode instance. Simply putting return null; or return Node; at the bottom of the method would solve that issue but you should think about what implications each option has furthur down the line.

if that particular if condition is true only it will have a value to return otherwise you need to give some other values to return in oter section ie

public TreeNode FindInstanceNode(string SubContainer,string instance)
        {
            TreeNode Node = GetTreeViewClickedNode();
            foreach (TreeNode trNode in Node.Nodes)
            {
                if (trNode.Text == "SUB-CONTAINERS")
                {
                    TreeNodeCollection tcSubNodes = trNode.Nodes;
                    foreach (TreeNode trInode in tcSubNodes)
                    {
                        TreeNodeCollection tcInsNodes = trInode.Nodes;
                        foreach (TreeNode InstanceNode in tcInsNodes)
                        {
                            if (InstanceNode.Text == "INSTANCE" && InstanceNode.FirstNode.Name == instance)
                            {
                                return InstanceNode;
                            }
                        }

                    }

                }

            }
          <here u want to give the return value  (give some garbage value as return value if condition does not satisfy for returning)>
        }

firstly, please use code tags when posting code, it will format it with correct indents to make nesting much easier to read :)

As for your error, a method that does not have a return type of void must return a value, even if it is null (provided your return type supports a null value). You are only returning a value when your if statements evaluate to true. You need to have a return value to handle the situation where an Instance Node is not found:

public TreeNode FindInstanceNode(string SubContainer, string returnNode)
        {
            TreeNode Node = GetTreeViewClickedNode();
            TreeNode returnNode = null;

            foreach (TreeNode trNode in Node.Nodes)
            {
                if (trNode.Text == "SUB-CONTAINERS")
                {
                    TreeNodeCollection tcSubNodes = trNode.Nodes;
                    foreach (TreeNode trInode in tcSubNodes)
                    {
                        TreeNodeCollection tcInsNodes = trInode.Nodes;
                        foreach (TreeNode InstanceNode in tcInsNodes)
                        {
                            if (InstanceNode.Text == "INSTANCE" && InstanceNode.FirstNode.Name == returnNode)
                            {
                                returnNode = InstanceNode;
                            }
                        }

                    }

                }

            }
            return returnNode;

        }

This will return the instance node if one is found, or null if there isnt one. You can tehn check for a null return in your calling code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.