1

I'm trying to implement this algorithm using Razor, but, I got this exception

System.IndexOutOfRangeException: Index was outside the bounds of the array.

@{ 
//....
for (int i = tab[0]; i <= tab[4]; i++)
        {
            if (i == pagination.numPageCourrante)
            {
                <li class="active"><a href="#">@i <span class="sr-only">(current)</span></a></li>
            }
            else
            {//from here the exception triggers
                <li><a href="/Accueil/Rechercher?rech=micro&type=nomAppMetier&num=@(tab[i])">@i </a></li>
            }

        }

}

Knowing that the declaration of the table is :

int[] tab = new int[5];

Thanks a lot !

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user3212730
  • 373
  • 3
  • 5
  • 13

4 Answers4

2

Seems like your logic is wrong.

What if your tab[4] will be 10? Your loop will work 10 times with this case but your array doesn't have 10 items. That's why probably you get IndexOutOfRangeException in your example. tab[4] will probably bigger than 4 and that's why your program try to access some index that your array doesn't have.

Arrays are zero-indexed. When you define an array with 5 items with;

int[] tab = new int[5];

You can access items indexed 0 to 4.

Sounds like you just need to use it like;

int[] tab = new int[5];
for (int i = 0; i < tab.Length ; i++)
{
    if (tab[i] == pagination.numPageCourrante)
    {
        //...
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

I suppose you don't want to use the values in the array as start and end of your for-loop. In order to make it work, you can do either of the following:

for (int i = 0; i < tab.Length; i++)
{
    var value = tab[i];
    // use value ...
}

Or, you can also use foreach:

foreach(int value in tab)
{
    // use value ...
}
Markus
  • 20,838
  • 4
  • 31
  • 55
0

Your different i values are : tab[0], tab[1], tab[2], tab[3], tab[4].

Then when you call tab[i] you actually call tab[tab[x]]. With x being successively 0, 1, 2, 3, 4. Then if any of your tab value is not in the interval [0, 4], for example 12, you try to reach tab[12] and IndexOutOFRangeException is thrown.

It is certainly where the mistake is if you want to simply loop through your array.

The two correct way to do it are with a for loop :

for (int i = 0; i < tab.Length; i++)
{
    // Your code here
}

or with a foreach loop :

foreach(int i in tab)
{
    // Your code here
}
Benjamin Bini
  • 311
  • 4
  • 15
0

if u enter like that...u can get the bound error in html array:

here the error happened in name

 <td style="width:10%"> @Html.DropDownList("InvoiceUnitID_"+@i, new SelectList(ViewBag.UnitOfMeasures, "Value", "Text", @Model.salesInvoiceVMList[i].InvoiceUnitID), htmlAttributes: new { id = "InvoiceUnitId", name = "InvoiceUnitID"[@i], @disabled="disabled" })</td>

instead of that:use like that

 <td style="width:10%"> @Html.DropDownList("InvoiceUnitID_"+@i, new SelectList(ViewBag.UnitOfMeasures, "Value", "Text", @Model.salesInvoiceVMList[i].InvoiceUnitID), htmlAttributes: new { id = "InvoiceUnitId", name = "InvoiceUnitID[@i]", @disabled="disabled" })</td>
Deepan Raj
  • 194
  • 3
  • 11