C#return关键字似乎无法正常工作,或者我在树视图遍历中失去理智和提示

时间:2014-02-25 19:53:11

标签: c# recursion return

我有return语句它应该退出函数但是当调试它到达return语句然后转到这个函数的最后一个大括号然后字面上跳转到if语句的else语句然后将递归然而,此函数将再次抛出异常,因为listview1.selecteditems [0] .tag为null! :-S

我做错了什么,失去了我的想法或两者兼而有之? (请暂时忽略functioncount变量)

编辑:一个人说返回语句的行为与它应该完全一样,因为它只结束当前函数的调用,而不是任何其他后续函数。我同意他的意思,但我的问题是我只调用过一次这个函数。一旦它返回它的事实立即跳转到else语句,在我看来它绕过整个函数?我不太确定......

    int functionCount = 0;
    private void openTreeViewNodeBasedOnListViewItem(string treeViewNodeToOpen,TreeNodeCollection nodes)
    {
        Console.WriteLine(functionCount);
        functionCount++;
        //if (treeViewNodeToOpen.Contains(@"\"))
        //{
        //    treeViewNodeToOpen = treeViewNodeToOpen.Substring(treeViewNodeToOpen.LastIndexOf(@"\") + 1);
        //    string[] fullNodePath = treeViewNodeToOpen.Split('\\');
        //}
        //Console.WriteLine("index {0}",listView1.SelectedItems[0].Index);
        string listViewItemAddress = listView1.SelectedItems[0].Tag.ToString();
        string treeViewItemAddress = "";

        listViewItemAddress = listViewItemAddress.Substring(listViewItemAddress.IndexOf('\\'));
        listViewItemAddress = @"Y:\" + listViewItemAddress;
        //Console.WriteLine("List view address {0}",listViewItemAddress);

        foreach (TreeNode node in nodes)
        {
            treeViewItemAddress = node.FullPath.ToString();
            if (node.Text == treeViewNodeToOpen && treeViewItemAddress == listViewItemAddress)
            {
                //Console.WriteLine("Tree view address {0}",node.FullPath.ToString());
                functionCount = 0;
                Console.WriteLine("list view {0} match found, navigating to tree node : {1}", listViewItemAddress, node.FullPath);
                treeView1_NodeMouseClickACTION(node);
                return; //when you return here it should exit the function however when debugging it goes out to the last curly brackets of this function and then jumps to the else statement of THIS if statement and then will recurse this function again however will throw an exception since the listview1.selecteditems[0].tag is null! :-S
            }
            else
            {
                Console.WriteLine("tree view node {1} does not match the node were looking for {0}", listViewItemAddress, node.FullPath);
                openTreeViewNodeBasedOnListViewItem(treeViewNodeToOpen, node.Nodes);
            }
        }

    } //after successfully finding a match and running the return statement, the debugger takes me here and then literally jumps to the else statement and I'm not quite sure why.

1 个答案:

答案 0 :(得分:2)

它完全按照预期工作。 return结束函数的当前调用,而不是调用堆栈上函数的每次调用。没有关键字可以做到这一点。

至于你的具体解决方案,它可以重新设计,以分离你想要完成的一些不同的任务,而不是在一个方法中完成所有这些,这是给你带来麻烦的一部分。

您正在做的一件事是遍历树以获取其中的所有节点。首先编写一个 的方法,而不做任何其他事情。这是树遍历的一个实现:

public static IEnumerable<T> Traverse<T>(T item, Func<T, IEnumerable<T>> childSelector)
{
    var stack = new Stack<T>();
    stack.Push(item);
    while (stack.Any())
    {
        var next = stack.Pop();
        yield return next;
        foreach (var child in childSelector(next))
            stack.Push(child);
    }
}

然后我们可以像这样调用它,以获得树中所有节点的平坦序列:

Traverse(root, node => node.Nodes.Cast<TreeNode>())

然后我们可以找到该序列中包含我们正在寻找的文本的第一个项目,并对该节点执行某些操作:

TreeNode root = GetRootNode();
var result = Traverse(root, node => node.Nodes.Cast<TreeNode>())
    .First(node => node.Text == "some text");
DoSomethingWithNode(result);