Skip to content
Loading
0
  • 0

    I think what's happened here is that you've got yourself into an infinite loop by resetting the for loop control variable 'i' to zero towards the bottom of the code you posted:

           if (i == 3 || x == itemsCount)
           {
              writer.Append("");
              i = 0; //** problem here
           }

    It's not normally a good idea to change the value of loop control variables from within the loop itself as it can lead to out of memory exceptions and other strange behaviour, so I'd rethink your logic here.

    One other point I've noticed is that you're using StringBuilder to avoid string concatenation but then concatenating some strings before appending them! Take this code for example:

           foreach (String str in pa.AttributeList)
           { writer.Append("

  • " + str.ToString().Trim() + "
  • "); } 

    It would be more sensible here to use 3 Append()'s:

           foreach (String str in pa.AttributeList)
           {
              writer.Append("

  • ");

              writer.Append(str.Trim()); // no need to use ToString() as str is already a string

              writer.Append("

  • ");
           }

    0
  • 0

    Thanks so much!  I followed your advice, Alan, and it's working perfectly now.

    Here's my new loop structure:

    int i = 0;

    foreach (PlantAttribute pa in items)

    {

    if (i == 0 || i == 8)

    { writer.Append(""); }

    writer.Append("");

    writer.Append(pa.AttributeName.Trim());

    writer.Append("

      ");

      foreach (String str in pa.AttributeList)

      {

      writer.Append("

    • ");

      writer.Append(str.Trim());

      writer.Append("

    • ");

      }

      writer.Append("

    ");

    if (i == 3 || i == 7 || i == itemsCount-1)

    {

    writer.Append("");

    }

    i++;

    }

    0