nested loop.. i dont know where is the error here

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
            for (int b = 0; b < fileCount.browseCount; b++ )
            {
                var rtb = tabControl1.TabPages[b].Controls.OfType<RichTextBox>().Single();

                for (int d = 0; d < fileCount.browseCount; d++)
                {
                    if (b != d)
                    {
                        var rtb2 = tabControl1.TabPages[d].Controls.OfType<RichTextBox>().Single();
                        int blc = 0;

                        string[] richarraycom = rtb.Text.Split(' ', '\n');
                        string[] richarraycom2 = rtb2.Text.Split(' ', '\n');

                        for (int a = 0; a < rtb.Lines.Count(); a++)
                        {
                            if (rtb.Lines.Count(string.IsNullOrEmpty) > rtb2.Lines.Count(string.IsNullOrEmpty))
                            {
                                blc = rtb.Lines.Count(string.IsNullOrEmpty) - rtb2.Lines.Count(string.IsNullOrEmpty);
                            }
                            if (rtb.Lines.Count(string.IsNullOrEmpty) <= rtb2.Lines.Count(string.IsNullOrEmpty))
                            {
                                blc = rtb2.Lines.Count(string.IsNullOrEmpty) - rtb.Lines.Count(string.IsNullOrEmpty);
                            }
                            int bl = 0;
                            int bld = 0;

                            foreach (string c in rtb.Lines[a].Split(' ', '\n'))
                            {
                                foreach (string v in rtb2.Lines[a].Split(' ', '\n'))
                                {
                                    if (rtb.Text.Contains(Environment.NewLine) && rtb2.Text.Contains(Environment.NewLine))
                                    {
                                        for (int add = 0; add < rtb2.Lines.Count(string.IsNullOrEmpty); add++)
                                        {
                                            if (string.IsNullOrWhiteSpace(c) && string.IsNullOrWhiteSpace(v))
                                            {
                                                bl++;
                                            }
                                        }
                                    }
                                    if (c == v)
                                    {
                                        n++;
                                    }
                                    bld = bl - blc;
                                    n = n - bld;
                                    if (rtb.Text.Count() > rtb2.Text.Count())
                                    {
                                        m = richarraycom.Length - sub;
                                    }
                                    if (rtb.Text.Count() <= rtb2.Text.Count())
                                    {
                                        m = richarraycom2.Length - sub;
                                    }

                                }
                            }
                        }
                    }
                    if(b == d || d == b)
                    {
                        d++;
                        MessageBox.Show(b.ToString() + "   " + d.ToString());
                    }
                    if (n > m && n != 0)
                    {
                        o = m / n * 100;
                    }
                    if (n <= m && m != 0)
                    {
                        o = n / m * 100;
                    }
                    n = 0;
                    m = 0;
                    label1.Text = o.ToString("#.##");
                }
            }


i dont know why the b and d variables always increment at the same time in every loop. all i want is, if the d variable is equal to the. the b variables will increment by 1. then if the b variables is equal to 10.. the loop will stop.
 
Last edited by a moderator:
That code is, I'm sorry to say, abysmal. You have numerous meaninglessly-named variables and not a single explanatory comment anywhere. We'd probably have to spend at least 5 minutes just to work out what the code is supposed to do before we could work out why it isn't doing it. It's really no wonder you can't work out what the issue is. I suggest that you overhaul it completely, debug it properly and add some comments to the code or at least an explanation of the code to your post if you expect us to volunteer our time to help you fix it.
 
here is with comments..
//this is the first loop variable
for (int b = 0; b < fileCount.browseCount; b++ )
{

    var rtb = tabControl1.TabPages[b].Controls.OfType<RichTextBox>().Single();
 //this is the second loop variable

    for (int d = 0; d < fileCount.browseCount; d++)
    {
             //condition for the two variables

        if (b != d)

        {
            var rtb2 = tabControl1.TabPages[d].Controls.OfType<RichTextBox>().Single();
            int blc = 0;
 
            string[] richarraycom = rtb.Text.Split(' ', '\n');
            string[] richarraycom2 = rtb2.Text.Split(' ', '\n');
                  //this loop is for the lines compasiron

            for (int a = 0; a < rtb.Lines.Count(); a++)
            {
                if (rtb.Lines.Count(string.IsNullOrEmpty) > rtb2.Lines.Count(string.IsNullOrEmpty))
                {
                    blc = rtb.Lines.Count(string.IsNullOrEmpty) - rtb2.Lines.Count(string.IsNullOrEmpty);
                }
                if (rtb.Lines.Count(string.IsNullOrEmpty) <= rtb2.Lines.Count(string.IsNullOrEmpty))
                {
                    blc = rtb2.Lines.Count(string.IsNullOrEmpty) - rtb.Lines.Count(string.IsNullOrEmpty);
                }
                int bl = 0;
                int bld = 0;
                    //this is for the two richtextboxes that i want to compare

                foreach (string c in rtb.Lines[a].Split(' ', '\n'))
                {
                    foreach (string v in rtb2.Lines[a].Split(' ', '\n'))
                    {
                        if (rtb.Text.Contains(Environment.NewLine) && rtb2.Text.Contains(Environment.NewLine))
                        {
                            for (int add = 0; add < rtb2.Lines.Count(string.IsNullOrEmpty); add++)
                            {
                                if (string.IsNullOrWhiteSpace(c) && string.IsNullOrWhiteSpace(v))
                                {
                                    bl++;
                                }
                            }
                        }
                        if (c == v)
                        {
                            n++;
                        }
                        bld = bl - blc;
                        n = n - bld;
                        if (rtb.Text.Count() > rtb2.Text.Count())
                        {
                            m = richarraycom.Length - sub;
                        }
                        if (rtb.Text.Count() <= rtb2.Text.Count())
                        {
                            m = richarraycom2.Length - sub;
                        }
 
                    }
                }
            }
        }
        if(b == d || d == b)
        {
            d++;
            MessageBox.Show(b.ToString() + "   " + d.ToString());
        }
           //this is for calculation the percentage of comparision


        if (n > m && n != 0)

        {
            o = m / n * 100;
        }
        if (n <= m && m != 0)
        {
            o = n / m * 100;
        }
        n = 0;
        m = 0;
        label1.Text = o.ToString("#.##");
    }
}




everytime i debug this.. the two variables increment at the same time..
i dont know what would it be the best solution for this..
 
Last edited by a moderator:
Are you actually debugging the code or are you just running the project in the debugger? Are you using a breakpoint and stepping through the code line by line, examining all the variable values as you go? If not then you haven't debugged. If you have debugged then you can, at the very least, point out to us EXACTLY where reality does not match your expectation and what all the variable values are at that time.
 
that code is running but i dont know.. why the variable b and d always incrementing at the same time.. :( i dont know why they always increment at the same time..
 
Let's take a step back, poringgunner can we get an an explanation of what this is supposed to do? Can you describe the steps of what this is supposed to do?
From my initial glance at it, are you comparing two lines at a time from the RTB control?
 
yes i comparing words inside the two lines at a time fron RTB.. i see the solution for that nested loop :) i just only add an condition then boom.. it works. hehe thanks for the help :) i really appreciate all your suggestion and helpful comments :)
 
Back
Top Bottom