Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I have Two Functions In which I am calling function one inside one ,It returns Actual output,But when Both the function returns Same value again and again then I want to Stop The Looping,But it doesn't Stop,It still Continue as an endless loop,So Please tell me how can I stop the looping?
My code is...
C#
protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID)
    {
        ArrayList listFamilyID = new ArrayList();

        var strings = RelativeMemberID.Cast<string>().ToArray();

        String s = String.Join(",", strings);
        
            //Find all FamilyID related to member
            sSQL = "SELECT DISTINCT AF_FamilyID FROM AllFamily_Master";
            sSQL = sSQL + " WHERE AF_MemberID IN(" + s + ")";
            

            DS = new DataSet();
            da = new OdbcDataAdapter(sSQL, conn);
            da.Fill(DS);
            
            if (DS.Tables.Count > 0)
            {
                if (DS.Tables["table"].Rows.Count > 0)
                {
                    for (int i = 0; i < DS.Tables["table"].Rows.Count; i++)
                    {
                        listFamilyID.Add(Convert.ToDouble(DS.Tables["table"].Rows[i]["AF_FamilyID"].ToString()));
                    }
                }
            }
         RelativeMemberIDOfFamily(listFamilyID);

       // CompareArrayList(listFamilyID, Convert.ToDouble(lblUserToFamilyID.Text.ToString()));
    }

    protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
        ArrayList listFamilyMemberID = new ArrayList();

        String t = String.Join(",", RelativeFamilyID.ToArray());
       
            sSQL = "SELECT DISTINCT AF_MemberID FROM AllFamily_Master";
            sSQL = sSQL + " WHERE AF_FamilyID IN(" + t + ")";

            DS1 = new DataSet();
            da1 = new OdbcDataAdapter(sSQL, conn);
            da1.Fill(DS1);

            if (DS1.Tables.Count > 0)
            {
                if (DS1.Tables["table"].Rows.Count > 0)
                {
                    for(int k=0;k<DS1.Tables["table"].Rows.Count;k++)
                    {
                        listFamilyMemberID.Add(DS1.Tables["table"].Rows[k]["AF_MemberID"].ToString());
                    }
                  
                }
            }
        
        RelativeFamilyIDFrom(listFamilyMemberID);
    }
Posted

I'm not quite sure what you are trying to do, but there is no way out with that code. Let me simplify it a bit by removing the "visual waste":
protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID)
    {
         ... Do something
         RelativeMemberIDOfFamily(listFamilyID);
    }
 
    protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
        ... Do something
        RelativeFamilyIDFrom(listFamilyMemberID);
    }
As soon as you call either method, it will always call the other, and will continue to do so until the stack runs out and your application crashes.
There is no code in either routine to prevent that under any circumstances.

Now, I can't say what they are doing, or when they should or shouldn't call each other, but you need to add a test of some form to prevent that at some point - or the infinite recursion will continue.

Personally, I think you actually need to sit down and think about what you have there, because the total sum of what these methods do in the real world is very slowly crash - they do not do anything with the data they retrieve except pass it to each other! It never leaves the method so you could best fix the problem by replacing your two methods with this:
protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID)
    {
    }
 
    protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
    }
And your app wouldn't crash, nor would there be any real change in how your application works...
 
Share this answer
 
You need to add a depth variable :
C#
protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID, ref int depth)
    {
          if(++depth >10) return; // stop at 10 levels deep for example
...
          RelativeMemberIDOfFamily(listFamilyID, ref depth);
}

protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
          if(++depth >10) return; // stop at 10 levels deep for example
...
          RelativeFamilyIDFrom(listFamilyMemberID, ref depth);
}


void main()
{
   int depth = 0; // start at 0
   RelativeFamilyIDFrom(listFamilyMemberID, ref depth); // call your first method
}
 
Share this answer
 
v2
Comments
SVT02 18-Feb-14 3:31am    
Thanks Mehdi i will try it
SVT02 18-Feb-14 4:29am    
still a same problem again

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