下面代码主要用来寻找指定标题的菜单项,问题是即使匹配了结果,也要继续循环下去!用goto?
       private void GetMenuItemById(ToolStripItemCollection p_itemCollection,string p_id)
        {
            foreach (ToolStripMenuItem item in p_itemCollection)
            {
                if (item.Text == p_id)
                {
                    result = item;
                    break;//不能简单通过它跳出循环
                }                if (item.HasDropDownItems && !bFind)
                {
                    GetMenuItemById(item.DropDownItems, p_id);
                }
            }
        }这种情况你们是怎么解决呀?

解决方案 »

  1.   

    break 可以跳出 foreach 的。
      

  2.   

    不是continue 可以继续循环吗。难道我没看懂题目
      

  3.   

    真看错了,是要跳出循环,那就要break;
      

  4.   

    你的问题好像说的不是很完整,我的印象当中用了foreach基本上没用break的。
    如果要继续循环,可以用循环,或者在if外层再加一层where()加以限制。
      

  5.   

    break跳出循环
    continue;进入下一轮循环,return 也能,goto也行,不过一般要少用,大家都不怎么使用
      

  6.   

    goto,直接了当,用,只要不滥,没有什么不可以
      

  7.   

    break只能退出当前着一层的循环,对于你这个递归的不能一下子全部退出,递归是在不断改变调用堆栈的,应该是只能让函数执行完了自己退栈吧,(这个我不太确定,按照常理推测而已……)
    看你有个bFind标志,是不是可以这样:      private void GetMenuItemById(ToolStripItemCollection p_itemCollection,string p_id) 
            { 
                foreach (ToolStripMenuItem item in p_itemCollection) 
                { 
                    if(bFind)
                        break; //如果已经找到了,直接退出循环,也就是退出这个函数,这将导致一系列的退栈操作,直到退出这个函数
                    if (item.Text == p_id) 
                    { 
                        result = item; 
                        bFind = true;  //找到了,设置标志
                        break;//不能简单通过它跳出循环 
                    }                 if (item.HasDropDownItems && !bFind) 
                    { 
                        GetMenuItemById(item.DropDownItems, p_id); 
                    } 
                } 
            } 
      

  8.   

    用return。虽然可能不会一次退出递归,但这会最快的返回。
      

  9.   

    方法应该加返回值嘛,表示找到了没,找到了就一层一层return true,可以快速弹栈
      

  10.   

    在 9 楼的兄弟的基础上更改了以下,结合 11楼兄弟的思想,就搞定。private bool GetMenuItemById(ToolStripItemCollection p_itemCollection,string p_id) 
            { 
                foreach (ToolStripMenuItem item in p_itemCollection) 
                { 
                    if(bFind)
                        break; //如果已经找到了,直接退出循环,也就是退出这个函数,这将导致一系列的退栈操作,直到退出这个函数
                    if (item.Text == p_id) 
                    { 
                        result = item; 
                        bFind = true;  //找到了,设置标志
                        break;//不能简单通过它跳出循环 
                    }                 if (item.HasDropDownItems && !bFind) 
                    { 
                        bFind = GetMenuItemById(item.DropDownItems, p_id); 
                    } 
                } 
            } 
      

  11.   

    private void GetMenuItemById(ToolStripItemCollection p_itemCollection,string p_id) 
            { 
    bool isout=false;
                foreach (ToolStripMenuItem item in p_itemCollection) 
                { 
                    if (item.Text == p_id) 
                    { 
                        result = item; 
    isout=true;
                        break;//不能简单通过它跳出循环 
                    }                 if (item.HasDropDownItems && !bFind) 
                    { 
                        GetMenuItemById(item.DropDownItems, p_id); 
                    } 
                } 
    if (isout) return result;
            } 
      

  12.   

     int[] intNums ={ 1, 2, 3, 4, 5 };
            foreach (int intNum in intNums)
            {
                if (intNum == 3)
                {
                    break;
                }
                Console.WriteLine(intNum);
            }
            Console.ReadKey();
      

  13.   

            private bool GetMenuItemById(ToolStripItemCollection p_itemCollection, string p_id)
            {
                foreach (ToolStripMenuItem item in p_itemCollection)
                {
                    if (item.Text == p_id)
                    {
                        result = item;
                        break;//不能简单通过它跳出循环 
                    }
                    if (GetMenuItemById(item.DropDownItems, p_id))
                        break;
                }
            }不用什么标志的
      

  14.   

    上面写错了,不好意思
            private bool GetMenuItemById(ToolStripItemCollection p_itemCollection, string p_id)
            {
                foreach (ToolStripMenuItem item in p_itemCollection)
                {
                    if (item.Text == p_id)
                    {
                        result = item;
                        return true;//不能简单通过它跳出循环 
                    }
                    if (GetMenuItemById(item.DropDownItems, p_id))
                        return true;
                }
                return false;
            }
      

  15.   

    是不是你看上去是一样的值,但是可能潜在的存在空格阿。。之类的。。所以我认为有必要trim下
      

  16.   

    for(;;)
      for(;;)
       for(;;)
        break;
    跳不出
    如果把break改成return;就结束整个函数而返回.
    使用goto Label 就可以去到在同一个scope下的任意一个Label:
      

  17.   

    把 break 改为 return;
      

  18.   

    goto嘿嘿。最近看到一家公司写的商业代码,一个函数几万行代码,外加若干个goto
      

  19.   


           private void GetMenuItemById(ToolStripItemCollection p_itemCollection,string p_id) 
            { 
                foreach (ToolStripMenuItem item in p_itemCollection) 
                { 
                    if (item.Text == p_id) 
                    { 
                        result = item; 
                        continue;//不能简单通过它跳出循环 
                    }                 if (item.HasDropDownItems && !bFind) 
                    { 
                        GetMenuItemById(item.DropDownItems, p_id); 
                    } 
                } 
            } 
      

  20.   

    ``break跳出最里面的一层循环```
     
    `continue跳出本次循环继续执行下一次循环```` return直接跳出,结束所以的语句`` ``goto也行,需要加标签  ````  只是算法太多容易混淆  ````` 至于楼主到底需要什么就自己选吧 ```````
      

  21.   

    我有一段代码,请高手解决:
    private bool  ComparStaffAccessAuth(TreeNodeCollection tc)
            {
                bool cflag = true;
                foreach (TreeNodeEx node in tc)
                {
                    if (node.Nodes.Count > 0)
                    {
                        ComparStaffAccessAuth(node.Nodes);                }
                    else
                    {
                        if (node.Text == "门1[1]" || node.Text == "门2[2]")
                        {
                            for (int i = 0; i < DTStaffVisitAuth.Rows.Count; i++)
                            {
                                if (DepStafftreeView.SelectedNode.Text == DTStaffVisitAuth.Rows[i]["StaffName"].ToString())
                                {
                                    if (DTStaffVisitAuth.Rows[i]["Channel"].ToString() == node.ID && node.Checked ==false)
                                    {
                                        cflag = false;                                    return cflag;
                                    }
                                }
                            }
                            if (node.Checked == true)
                            {
                                cflag = false;
                                for (int j = 0; j < DTStaffVisitAuth.Rows.Count; j++)
                                {
                                    if (DepStafftreeView.SelectedNode.Text == DTStaffVisitAuth.Rows[j]["StaffName"].ToString() && DTStaffVisitAuth.Rows[j]["Channel"].ToString() == node.ID)
                                    {
                                       cflag = true;
                                       return cflag;                                }
                                }
                            }
                            
                        }
                       
                    }
                }
                return cflag;
            }
    这段代码,循环到最后的结果永远都是true,怎么让return的结果与条件中的return结果一致