本文介绍了如何在c#中停止执行递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨我想停止执行当前的递归函数,但循环重复使用相同的值,我尝试使用下面的代码 protected void BottomSearch( String AncestorFamilyID,ArrayList RelationPath) { ArrayList bottomMembers = new ArrayList(); ArrayList l2 = new ArrayList(); int depth = 0 ; if (++ depth < 10 ) { var aff = AncestorFamilyID.Cast< string>()。ToArray(); 字符串 AFamilyID = 字符串 .Join( ,,aff); sSQL = SELECT DISTINCT AF_MemberID FROM AllFamily_Master; sSQL = sSQL + WHERE AF_FamilyID IN( + AncestorFamilyID + ); DS2 = new DataSet(); da2 = new OdbcDataAdapter(sSQL,conn); da2.Fill(DS2); if (DS2.Tables.Count > 0 ) { if (DS2.Tables [ table]。Rows.Count > 0 ) { for ( int i = 0 ; i < DS2.Tables [ table]。Rows.Count; i ++) { bottomMembers.Add(DS2.Tables [ table]。行[i] [ AF_MemberID]的ToString()); } } } DS2.Dispose(); da2.Dispose(); 字符串 m1 = 字符串 .Join( ,,bottomMembers.Cast< string>()。ToArray()); sSQL = SELECT AF_FamilyID FROM AllFamily_Master; sSQL = sSQL + WHERE AF_MemberID IN( + m1 + ); sSQL = sSQL + AND AF_MemberType IN('H'); DS3 = new DataSet(); da3 = new OdbcDataAdapter(sSQL,conn); da3.Fill(DS3); if (DS3.Tables.Count > 0 ) { if (DS3.Tables [ table]。Rows.Count > 0 ) { for ( int k = 0 ; k < DS3.Tables [ table]。Rows.Count; k ++) { BottomSearch(DS3.Tables [ table]。行[k] [ AF_FamilyID]。的ToString(),Relati onPath); } } } DS3.Dispose(); da3.Dispose(); if (CompareArrayList(bottomMembers,(lblUserToMemberID.Text.ToString()))) { List<串GT; str1 = FindTargetMember(bottomMembers,lblUserToMemberID.Text.ToString()); RelationPath.Add(str1); return ; } } 解决方案 这总是递归的。 您可能要做的是将深度变量传递给您的方法。现在,在你的方法中你这样做: int depth = 0 ; if (++ depth < 10 ){ / * ... * / } 因此if语句总是如此。你应该做的是传递depth参数做你的递归方法,作为ref参数。像这样: 受保护 void BottomSearch ( String AncestorFamilyID,ArrayList RelationPath, ref int 深度) { if (++ depth < 10 ){} } 当深度为10时,它'不要输入if语句,不再以递归方式调用此方法。 Hi I want to stop the execution of current recursive function,but the loop repeats again and again with the same value,I have tried with below code protected void BottomSearch(String AncestorFamilyID, ArrayList RelationPath) { ArrayList bottomMembers = new ArrayList(); ArrayList l2 = new ArrayList(); int depth = 0; if (++depth < 10) { var aff = AncestorFamilyID.Cast<string>().ToArray(); String AFamilyID = String.Join(",", aff); sSQL = "SELECT DISTINCT AF_MemberID FROM AllFamily_Master"; sSQL = sSQL + " WHERE AF_FamilyID IN(" + AncestorFamilyID + ")"; DS2 = new DataSet(); da2 = new OdbcDataAdapter(sSQL, conn); da2.Fill(DS2); if (DS2.Tables.Count > 0) { if (DS2.Tables["table"].Rows.Count > 0) { for (int i = 0; i < DS2.Tables["table"].Rows.Count; i++) { bottomMembers.Add(DS2.Tables["table"].Rows[i]["AF_MemberID"].ToString()); } } } DS2.Dispose(); da2.Dispose(); String m1=String.Join(",",bottomMembers.Cast<string>().ToArray()); sSQL = "SELECT AF_FamilyID FROM AllFamily_Master"; sSQL = sSQL + " WHERE AF_MemberID IN("+m1+")"; sSQL = sSQL + " AND AF_MemberType IN('H')"; DS3 = new DataSet(); da3 = new OdbcDataAdapter(sSQL, conn); da3.Fill(DS3); if (DS3.Tables.Count > 0) { if (DS3.Tables["table"].Rows.Count > 0) { for (int k = 0; k < DS3.Tables["table"].Rows.Count; k++) { BottomSearch(DS3.Tables["table"].Rows[k]["AF_FamilyID"].ToString(),RelationPath); } } } DS3.Dispose(); da3.Dispose(); if (CompareArrayList(bottomMembers, (lblUserToMemberID.Text.ToString()))) { List<string> str1 = FindTargetMember(bottomMembers, lblUserToMemberID.Text.ToString()); RelationPath.Add(str1); return; }} 解决方案 This is always going to be recursive.What you probably meant to do is pass the depth variable to your method. Right now, in your method you do this:int depth = 0;if (++depth < 10) { /*...*/ }So that if statement is always going to be true. What you should do is pass the depth parameter do your recursive method, as a ref parameter. Like this:protected void BottomSearch(String AncestorFamilyID, ArrayList RelationPath, ref int depth){ if (++depth < 10) {}}When depth is 10, it'll not enter the if statement, and no longer will call this method recursively. 这篇关于如何在c#中停止执行递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 05:18