且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c#中停止执行递归函数

更新时间:2022-10-19 12:55:14

这总是递归的。



您可能要做的是将深度变量传递给您的方法。现在,在你的方法中你这样做:

  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.