Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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;
            }
           
		}
Posted
Updated 21-Feb-14 23:55pm
v3
Comments
Maarten Kools 22-Feb-14 5:47am    
Uhm, where's the recursive part?
Abhinav S 22-Feb-14 5:53am    
Where is your code going into a recursive loop?
SVT02 22-Feb-14 5:56am    
Sorry,Please see my updated code

1 solution

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:
C#
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:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900