0

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

                    }

                }

            }

        }

Edited by Nick Evan: Fixed formatting

4
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by Geekitygeek
Featured Replies
  • 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 [B]must[/B] return a value, even if it is null (provided your return … Read More

0
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.

0

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

Edited by Nick Evan: Fixed formatting

2

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.

Edited by Geekitygeek: typo

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.