Why my loop does't work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dancgr
    New Member
    • Jan 2010
    • 8

    Why my loop does't work?

    Well, my loop is correct, but it simples stops at the first ocurrence... Can canybody help me??

    foreach (string word2 in words)
    {
    if (wordl[i] == word2)
    {
    i++;
    MessageBox.Show ("1");
    }
    else if (word2 == null)
    {
    MessageBox.Show ("2");
    }
    else if ((word2.Split(' ='))[1] == (0).ToString())
    {
    MessageBox.Show ("3");
    }
    else
    {
    MessageBox.Show ("4");
    download[j] = word2;
    i++;
    j++;
    }
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please enclose your source code in CODE tags (by highlighting the text and pressing the "#" button). This makes it easier for others to read.

    What language is this program written in? It certainly isn't C; and I don't think C++ has a 'foreach' keyword either.

    That said ... how sure are you that words contains more than one string? You might try printing out the value of word2 for each pass through the loop to make sure it is what you expect.

    Comment

    • dancgr
      New Member
      • Jan 2010
      • 8

      #3
      It is c#.
      The problem is that the loop occurs once, but when it is suposed to loop(start again) it stops.
      I reformulated it:
      Code:
                  for(; i<=counter; i++)
                  {
                      if (wordl[i] == null)
                      continue;
                      if (words[i] == wordl[i])
                      {
                          MessageBox.Show("1");
                      }
                      else if (words[i] == null)
                      { 
                          MessageBox.Show("2"); 
                      }
                      else if ((words[i].Split('='))[1] == "0") 
                      { 
                          MessageBox.Show("3"); 
                      } 
                      else 
                      { 
                          MessageBox.Show("4");
                          download[j] = words[i]; 
                          j++; 
                      } 
                      MessageBox.Show("passo loop");
                  }
      And before this code, i ran:
      Code:
                  foreach (string word in words)
                  {
                      MessageBox.Show(word);
                      counter++;
                  }
      And checked everything.

      Comment

      • Time
        New Member
        • Jan 2010
        • 77

        #4
        Values of i and counter may be getting equal or counter may be becoming greater than i. May be debugging will help..

        Comment

        • dancgr
          New Member
          • Jan 2010
          • 8

          #5
          The problem is tha debuggin don't result in any errors. It simply works, but in the loop it stops without getting any error...

          Comment

          Working...