BuzzLightYear
BuzzLightYear

Reputation: 49

Looping through an array dynamically

I am currently looping through an array successfully to populate a table in asp.net using visual basic. I want to populate the first row with the first 9 items in the array before populating the next row from the current position of the counter. I am only 2 days into using vb and my current loop populates each row with the index 0 - 9 but ideally want to populate the next row starting from 10 - 18 and so on. In C# I have accomplished this using the code shown below:

<div class="col-md-10 col-md-offset-1">

    <h3 style="text-align:center; padding: 0px;">List of Alerts for Today</h3>
    <table id="myTable" class="tablesorter" style="width:100%">
        <thead class="finger_hover">
            <tr>
                <th>Sensor</th>
                <th>Date Time</th>
                <th>Source IP</th>
                <th>Source Port</th>
                <th>Destination IP</th>
                <th>Destination Port</th>
                <th>Protocol</th>
                <th>Signature</th>
                <th>Signature Class</th>
                <th>Count</th>

            </tr>
        </thead>

        <tbody>

            @{

                int i;
                for (i = 0; i < alertForTheDay.Count; i++)
                {


                    @:<tr>
                        @:
                        <td>@alertForTheDay[i]</td>
                                @:
                                <td>@alertForTheDay[i + 1]</td>
                                @:
                                <td>@alertForTheDay[i + 2]</td>
                                @:
                                <td>@alertForTheDay[i + 3]</td>
                                @:
                                <td>@alertForTheDay[i + 4]</td>
                                @:
                                <td>@alertForTheDay[i + 5]</td>
                                @:
                                <td>@alertForTheDay[i + 6]</td>
                                @:
                                <td>@alertForTheDay[i + 7]</td>
                                @:
                                <td>@alertForTheDay[i + 8]</td>
                                @:
                                <td>@alertForTheDay[i + 9]</td>


                                @:</tr>

                    i = i + 9;

                }


            }

        </tbody>

    </table>

    </div>

This code populates the next row with the next set of items in the array. Here is the code for vb:

  <div class="col-md-10 col-md-offset-1">

    <h3 style="text-align:center; padding: 0px;">List of Alerts for Today</h3>
    <table id="myTable" class="tablesorter" style="width:100%">
        <thead class="finger_hover">
            <tr>
                <th>Sensor</th>
                <th>Date Time</th>
                <th>Source IP</th>
                <th>Source Port</th>
                <th>Destination IP</th>
                <th>Destination Port</th>
                <th>Protocol</th>
                <th>Signature</th>
                <th>Signature Class</th>
                <th>Count</th>

            </tr>
        </thead>

        <tbody>

            @code
                Dim increment As Integer = 9
                Dim i As Integer
                For i = 0 To alertForTheDay.Count Step i + 1

                    @:<tr>

                        @:<td>@alertForTheDay(0)</td>
                        @:<td>@alertForTheDay(1)</td>
                        @:<td>@alertForTheDay(2)</td>
                        @:<td>@alertForTheDay(3)</td>
                        @:<td>@alertForTheDay(4)</td>
                        @:<td>@alertForTheDay(5)</td>
                        @:<td>@alertForTheDay(6)</td>
                        @:<td>@alertForTheDay(7)</td>
                        @:<td>@alertForTheDay(8)</td>
                        @:<td>@alertForTheDay(9)</td>

                        @:</tr>

                    i = i + 9

                Next

            End code

        </tbody>

    </table>

</div>

The vb code populates the cell with the exact index and even when I step through the code and the number changes, the cell is always populated with the exact value referenced. I am sure there is an easy way of doing this. Please any help is appreciated. Thanks

Upvotes: 0

Views: 114

Answers (1)

Steve
Steve

Reputation: 216302

Just change your loop to increment by 10 and use the indexer of the loop to retrieve your values from the array

           For i = 0 To alertForTheDay.Count -1 Step 10

                @:<tr>

                    @:<td>@alertForTheDay(i)</td>
                    @:<td>@alertForTheDay(i+1)</td>
                    @:<td>@alertForTheDay(i+2)</td>
                    @:<td>@alertForTheDay(i+3)</td>
                    @:<td>@alertForTheDay(i+4)</td>
                    @:<td>@alertForTheDay(i+5)</td>
                    @:<td>@alertForTheDay(i+6)</td>
                    @:<td>@alertForTheDay(i+7)</td>
                    @:<td>@alertForTheDay(i+8)</td>
                    @:<td>@alertForTheDay(i+9)</td>

                    @:</tr>

            Next

Of course this assumes that you alertForTheDay contains a number of elements always a multiple of 10

What I have fixed:

  • The loop exit condion should be 1 less than the count of the elements because arrays start at index 0
  • The increment step is of 10 elements (the number of elements required by one of your rows
  • The element is extracted from the array using the value of i as base and incrementing this value to reach every element in blocks of 10
  • Removed the final increment of i (the Step 10 in the for loop makes this useless)

Upvotes: 1

Related Questions