locked
Index out of range when in for loop but works in foreach RRS feed

  • Question

  • User-1453561518 posted

    This works:

                     foreach (MenuHeader header in MenuHeaders){
                        html += "<tr>";
                        foreach (MenuItem item in header.MenuItems){
                        
                                html += item.getHTML();
                        }
                        html += "</tr>";
                    }

    This Doesnt:

    for(int i = 0; i <= 3; i++){
                        foreach (MenuHeader header in MenuHeaders){
                            
                            html += "<tr>";
                            html += header.MenuItems[i].getHTML();
                            html += "</tr>";
                        }
                    }

    Does anyone have any clue why?

    Wednesday, August 27, 2014 11:54 AM

Answers

  • User-1453561518 posted

    Thanks for your help I got it:

    I swear I tried that line before but o-well. THANKS!

    html += "<table><tbody><tr>";
                    foreach (MenuHeader header in MenuHeaders){
                        html += header.getHTML();
                    }
                    html += "</tr>";
                    for(int i = 0; i <= 1; i++){
                        html += "<tr>";
                        foreach (MenuHeader header in MenuHeaders){
                            if (i < header.MenuItems.Count) html +=  header.MenuItems[i].getHTML();
                        }
                        html += "</tr>";
                    }
                    html += "</table></tbody>";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 27, 2014 12:12 PM

All replies

  • User-1360095595 posted

    Use < instead of <=. Or use MenuHeaders.MenuItems.Count instead of 3 and also use less than < instead of less than or equal <=

    Wednesday, August 27, 2014 12:00 PM
  • User-718146471 posted

    I'm going to recommend you try using the debugger and set a breakpoint at the for(...) line and step through it to see what happens.  I have a suspicion that the problem is the value of header is staying the same so it may blow up when it tries to go through the second time.  YOu may need to move the code that sets the value of header to inside the for loop.

    Wednesday, August 27, 2014 12:01 PM
  • User2103319870 posted

    Are you sure you have 4 values(0,1,2,3,) in header.MenuItems array. If not you will face this issue.

    Alternatively if you have only four values in your array then you don't need to have the out for loop you can fetch records like given below

    foreach (MenuHeader header in MenuHeaders){
                            
                            html += "<tr>";
                            html += header.MenuItems[0].getHTML();
     			html += header.MenuItems[1].getHTML();
    			html += header.MenuItems[2].getHTML();
    			html += header.MenuItems[3].getHTML();
                            html += "</tr>";
                        }

    EDIT: Corrected the sentence

    Wednesday, August 27, 2014 12:04 PM
  • User-1453561518 posted

    Hmm well here is what this outputs so header does change.:

    Also, I do know about using < and the List.count way. I'm using set numbers for known limits for debugging purposes.

    Is there a debugger in WebMatrix?

    File Edit Format View Help
    0 0 0 0 0
    1 1 1 1 1
    2 2 2 2 2
    3 3 3 3 3

    html += "<table><tbody><tr>";
                    foreach (MenuHeader header in MenuHeaders){
                        html += header.getHTML();
                    }
                    html += "</tr>";
                    for(int i = 0; i <= 3; i++){
                        html += "<tr>";
                        foreach (MenuHeader header in MenuHeaders){
                            
                            html += "<td>" + i.ToString() + "</td>";
                        }
                        html += "</tr>";
                    }
                    html += "</table></tbody>";
    Wednesday, August 27, 2014 12:05 PM
  • User-1453561518 posted

    Another thing that may be of help is this is what is generated:

    File Edit Format View Help
    New Open Save
    Cut Copy Paste
    Word Wrap Font
    Status Bar
    View Help

    From this:

     html += "<table><tbody><tr>";
                    foreach (MenuHeader header in MenuHeaders){
                        html += header.getHTML();
                    }
                     html += "</tr>";
                    foreach (MenuHeader header in MenuHeaders){
                        html += "<tr>";
                        foreach (MenuItem item in header.MenuItems){
                        
                                html += item.getHTML();
                        }
                        html += "</tr>";
                    }
                    html += "</table></tbody>";
    Wednesday, August 27, 2014 12:08 PM
  • User-1453561518 posted

    Thanks for your help I got it:

    I swear I tried that line before but o-well. THANKS!

    html += "<table><tbody><tr>";
                    foreach (MenuHeader header in MenuHeaders){
                        html += header.getHTML();
                    }
                    html += "</tr>";
                    for(int i = 0; i <= 1; i++){
                        html += "<tr>";
                        foreach (MenuHeader header in MenuHeaders){
                            if (i < header.MenuItems.Count) html +=  header.MenuItems[i].getHTML();
                        }
                        html += "</tr>";
                    }
                    html += "</table></tbody>";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 27, 2014 12:12 PM