C#中跳出递归循环的方式,请各位高手们不吝赐教啊!

PCI_E 2009-06-05 04:07:42
下面是我的递归方法,我想要在方法找到满足的条件后就跳出循环,但是试了几种方法还是没有成功。请大家帮帮忙,出出主意

private void FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
this.treeList1.FocusedNode = node;

}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
}
}

}
...全文
2102 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwei466 2009-06-06
  • 打赏
  • 举报
回复
可以设置一个跳转代码。
lextm 2009-06-06
  • 打赏
  • 举报
回复
看起来lz的代码是遍历所有结点,找出所有的匹配。(当然,那就还需要传一个List进去,把找到的结点都加到这个列表里面)

可是为什么lz要求中途退出呢?那你的代码逻辑肯定就不对了。

那么建议添加一个新的参数这样应该就不需要全局参数了。毕竟全局参数是OO的敌人。

private void FocusHasPicNode(TreeListNodes nodes, TreeListNode found)
{
if(found != null)
return;
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
found = node;
return;
}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes, found);
}
}
}

}
aight 2009-06-05
  • 打赏
  • 举报
回复
全局变量
xp1056 2009-06-05
  • 打赏
  • 举报
回复
利用全局变量来标记是否改结束然后
return;
csbinchina 2009-06-05
  • 打赏
  • 举报
回复

bool bStatus = true;
private void FocusHasPicNode(TreeListNodes nodes)
{
if(!bStatus)
return;
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
this.treeList1.FocusedNode = node;
bStatus = false;

}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
}
}

}
my_2008wing 2009-06-05
  • 打赏
  • 举报
回复
如果break不行的话
设置个bool变量可能也不行,可以调试一下,当执行到break时的情况,可能跳出条件没有设好
PCI_E 2009-06-05
  • 打赏
  • 举报
回复
看来只能是设置全局变量来控制了,return和break诗无法在符合第一次后就退出循环了的
qqiuzaihui 2009-06-05
  • 打赏
  • 举报
回复
加一个静态的布尔变量:

static bool bState = true;
private void FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
this.treeList1.FocusedNode = node;
bState = false; //设置状态

}
else
{
if( bState )
{
if (node.Nodes.Count > 0 )
{
FocusHasPicNode(node.Nodes);
}
}
else
{
break;
}
}
}

}
jishengzu 2009-06-05
  • 打赏
  • 举报
回复

//你把方法体改成有返回值的 在调用FocusHasPicNode()这方法那里为treeList1.FocusedNode赋值
//如treeList1.FocusedNode=FocusHasPicNode(node);
private DevExpress.XtraTreeList.Nodes.TreeListNode FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if ((!string.IsNullOrEmpty(sIsRead)) && (sIsRead.Equals("1")) && (!string.IsNullOrEmpty(sPicKind)) && (!sPicKind.StartsWith("a")) && (!sPicKind.StartsWith("b")) && (node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
//this.treeList1.FocusedNode = node;
return node;

}
else
{
if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}
}
}

}
llsen 2009-06-05
  • 打赏
  • 举报
回复
int i=0;

while(true)
{
//dosomething
if(i++ == 100)
break;
}
nszhang 2009-06-05
  • 打赏
  • 举报
回复
break就可以



流年 2009-06-05
  • 打赏
  • 举报
回复
加个全局变量,判断的地方把全局变量也作为条件之一,如果第一个节点更新了,就跳出,同时修改这个变量,其他节点在更新的时候,判断这个变量,如果确定节点找到了,就不执行任何操作,应该能的到,但是效率还是有点问题,呵呵
mdq001 2009-06-05
  • 打赏
  • 举报
回复
如果这样,设置一个bool变量,符合条件则设置bool为true,再else里判断bool是否为false,如果为false则递归方法,如果为true则不进入else语句块,自然不会递归方法了
PCI_E 2009-06-05
  • 打赏
  • 举报
回复 1
mdq001
你好,我的意思是在这个递归循环中满足我条件的节点有很多,但是我只想在找到第一个节点后就退出,但是我用break和return都无法实现这个目的
mdq001 2009-06-05
  • 打赏
  • 举报
回复
把思路反过来,如果符合条件则继续,否则跳出


private void FocusHasPicNode(TreeListNodes nodes)
{
foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
{
string sIsRead = (node.GetValue("A") != null) ? node.GetValue("A").ToString() : string.Empty;
string sPicKind = (node.GetValue("B") != null) ? node.GetValue("B").ToString() : string.Empty;
if (!(!string.IsNullOrEmpty(sIsRead)) && !(sIsRead.Equals("1")) && !(!string.IsNullOrEmpty(sPicKind)) && !(!sPicKind.StartsWith("a")) && !(!sPicKind.StartsWith("b")) && !(node.Visible))
{
//进入这个条件就退出循环,我用return和break都无法直接退出,方法还是会继续运行下去
//this.treeList1.FocusedNode = node;

if (node.Nodes.Count > 0)
{
FocusHasPicNode(node.Nodes);
}


}

}

}


hendriclee 2009-06-05
  • 打赏
  • 举报
回复
break跳出

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧