Improve Article
Save Article
Improve Article
Save Article
C# supports the creation and manipulation of arrays, as a data structure. The index of an array is an integer value that has value in the interval [0, n-1], where n is the size of the array. If a request for a negative or an index greater than or equal to the size of the array is made, then the C# throws an System.IndexOutOfRange Exception. This is unlike C/C++ where no index of the bound check is done. The IndexOutOfRangeException is a Runtime Exception thrown only at runtime. The C# Compiler does not check for this error during the compilation of a program.
Example:
using
System;
public
class
GFG {
public
static
void
Main(String[] args)
{
int
[] ar = {1, 2, 3, 4, 5};
for
(
int
i = 0; i <= ar.Length; i++)
Console.WriteLine(ar[i]);
}
}
Runtime Error:
Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at GFG.Main (System.String[] args) <0x40bdbd50 + 0x00067> in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at GFG.Main (System.String[] args) <0x40bdbd50 + 0x00067> in :0
Output:
1 2 3 4 5
Here if you carefully see, the array is of size 5. Therefore while accessing its element using for loop, the maximum value of index can be 4 but in our program, it is going till 5 and thus the exception.
Let’s see another example using ArrayList:
using
System;
using
System.Collections;
public
class
GFG {
public
static
void
Main(String[] args)
{
ArrayList lis =
new
ArrayList();
lis.Add(
"Geeks"
);
lis.Add(
"GFG"
);
Console.WriteLine(lis[2]);
}
}
Runtime Error: Here error is a bit more informative than the previous one as follows:
Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item (Int32 index) <0x7f2d36b2ff40 + 0x00082> in :0
at GFG.Main (System.String[] args) <0x41b9fd50 + 0x0008b> in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item (Int32 index) <0x7f2d36b2ff40 + 0x00082> in :0
at GFG.Main (System.String[] args) <0x41b9fd50 + 0x0008b> in :0
Lets understand it in a bit of detail:
- Index here defines the index we are trying to access.
- The size gives us information of the size of the list.
- Since size is 2, the last index we can access is (2-1)=1, and thus the exception
The correct way to access array is :
for (int i = 0; i < ar.Length; i++) { }
Handling the Exception:
- Use for-each loop: This automatically handles indices while accessing the elements of an array.
- Syntax:
for(int variable_name in array_variable) { // loop body }
- Syntax:
- Use Try-Catch: Consider enclosing your code inside a try-catch statement and manipulate the exception accordingly. As mentioned, C# won’t let you access an invalid index and will definitely throw an IndexOutOfRangeException. However, we should be careful inside the block of the catch statement, because if we don’t handle the exception appropriately, we may conceal it and thus, create a bug in your application.
In this article, we are going to learn about one of the most common exceptions in C# – IndexOutOfRangeException.
To download the source code for this article, you can visit our GitHub repository.
We’re going to learn about various situations where this exception gets raised and about the preventive measures we can take to make our applications more robust.
IndexOutOfRangeException
gets thrown when we try to access a member of an array or a collection using an invalid index.
An invalid index means either the index is lower than the collection’s lower bound or greater than/equal to the number of elements in the collection.
We are going to cover some common examples of where IndexOutOfRangeException
is raised.
Another exception you’re familiar with is NullReferenceException
. If you want to learn more, we have an article about NullReferenceException as well.
Array – Upper Bound
The upper bound of a zero-based array – should be one less than the array length.
Let’s create an integer array and run a for loop on it to demonstrate the exception:
var numbersArray = new int[] { 1, 2, 3, 4, 5 }; for (var i = 0; i <= numbersArray.Length; i++) { Console.WriteLine(numbersArray[i]); }
The for loop runs perfectly fine when i < 5
, but it fails for i == 5
because there is no element at the position 6 (index 5 – {0, 1, 2, 3, 4 , 5}):
1 2 3 4 5 Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
How to Fix IndexOutOfRangeException?
Now, let’s see how we can fix IndexOutOfRangeException
in this case.
Let’s remove the equality condition and just check for i < numbersArray.Length
:
var numbersArray = new int[] { 1, 2, 3, 4, 5 }; for (var i = 0; i < numbersArray.Length; i++) { Console.WriteLine(numbersArray[i]); }
And if we run the application again, there’s no exception this time:
1 2 3 4 5
Array – Custom Lower Bound
We can create an array with a custom lower bound by using the Array.CreateInstance(Type elementType, int[] lengths, int[] lowerBounds)
method:
var customLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 }); var value = 2; for (var i = 0; i < customLowerBoundArray.Length; i++) { customLowerBoundArray.SetValue(value, i); value *= 5; }
The code snippet throws IndexOutOfRangeException
when we try to set the array element’s value at an index lower than the custom lower bound.
The lower bound is 1, but the first element is set at position 0 (i == 0
).
Solution
Let’s see how we can prevent the exception in this case:
Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<
var customLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 }); var value = 2; for (var i = customLowerBoundArray.GetLowerBound(0); i <= customLowerBoundArray.GetUpperBound(0); i++) { customLowerBoundArray.SetValue(value, i); value *= 5; } for (var i = customLowerBoundArray.GetLowerBound(0); i <= customLowerBoundArray.GetUpperBound(0); i++) { Console.WriteLine(customLowerBoundArray.GetValue(i)); }
We make use of Array.GetLowerBound()
and Array.GetUpperBound()
methods to determine the start and end index of the custom lower bound array:
2 10 50 250 1250
List – Incorrect Arguments
This scenario is a little bit different:
Console.WriteLine("Please enter a search argument: "); var searchArg = Convert.ToInt32(Console.ReadLine()); var multiplesOfFive = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 }; Console.WriteLine($"Let's display the multiples of 5 greater than {searchArg}"); var startIndex = multiplesOfFive.IndexOf(searchArg); for (var i = startIndex; i < multiplesOfFive.Count; i++) { Console.WriteLine(multiplesOfFive[i]); }
We have a List<int>
to which we add the first 10 multiples of 5.
We take input from the user – searchArg
, and use this input to display numbers greater than the input.
An input other than the numbers present in the list results in an index of -1, which raises the ArgumentOutOfRangeException
.
An input of 12 breaks the code and we get the exception:
Please enter a search argument: 12 Let's display the even numbers greater than 12 Unhandled exception. System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Solution
To prevent the exception, we can add a check:
Console.WriteLine("Please enter a search argument!!!"); var searchArg = Convert.ToInt32(Console.ReadLine()); var multiplesOfFive = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 }; var startIndex = multiplesOfFive.IndexOf(searchArg); if (startIndex < 0) { Console.WriteLine($"The number {searchArg} does not exist in the list."); } else { Console.WriteLine($"Let's display the even numbers greater than {searchArg}"); for (var i = startIndex; i < multiplesOfFive.Count; i++) { Console.WriteLine(multiplesOfFive[i]); } }
An incorrect input (other than the numbers in the list) results in a user-friendly message:
Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<
Please enter a search argument: 22 The number 22 does not exist in the list.
IndexOutOfRangeException vs ArgumentOutOfRangeException
Lists raise ArgumentOutOfRangeException
when an item is accessed at an invalid index, whereas arrays raise IndexOutOfRangeException
for the same behavior:
IList<string> tempList = new string[] { "0" }; var foo = tempList[-1]; //ArgumentOutOfRangeException string[] tempArray = new string[] { "0" }; var bar = tempArray[-1]; //IndexOutOfRangeException
Two Arrays – Different Lengths
The IndexOutOfRangeException
is raised when we attempt to assign an array element to a different array that has fewer elements than the first array:
var array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var array2 = new int[7]; array2[array1.Length - 1] = array1[array1.Length - 1];
The array1
length is 10, while the array2
length is 7.
We are trying to assign the last element of array1
to array2
, but since array2
is smaller (length is 7), we get the IndexOutOfRangeException
.
Solution
Now, let’s see how we can prevent the exception:
var array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var array2 = new int[array1.Length]; array2[array1.Length - 1] = array1[array1.Length - 1]; Console.WriteLine($"The last element of array2 is: {array2[array2.Length - 1]}");
We initialize the second array as similar to the first array’s length:
The last element of array2 is: 10
Getting Confused Between an Index and the Value of That Index
Let’s see this case with an example:
Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<
var oddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; foreach (var number in oddNumbersArray) { Console.WriteLine($"The odd number is: {oddNumbersArray[number]}"); }
We have an array of odd numbers, we run a foreach loop on the array and try to access the element using the index and not the element itself.
We start encountering IndexOutOfRangeException
when the number == 11
acts as the index.
There is no element at the 11th index:
The odd number is: 3 The odd number is: 7 The odd number is: 11 The odd number is: 15 The odd number is: 19 Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Solution
We can prevent the exception easily by using the value itself, instead of trying to access the value by index in each iteration:
var oddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; foreach (var number in oddNumbersArray) { Console.WriteLine($"The odd number is: {number}"); }
This effectively prevents the exception:
The odd number is: 1 The odd number is: 3 The odd number is: 5 The odd number is: 7 The odd number is: 9 The odd number is: 11 The odd number is: 13 The odd number is: 15 The odd number is: 17 The odd number is: 19
DataTable – Incorrect Column Index
We create a method to render a static DataTable in a separate StaticDataRepository
class:
public class StaticDataRepository { public static DataTable GetGroceries() { var dataTable = new DataTable { TableName = "Groceries" }; dataTable.Columns.Add("Id", typeof(int)); dataTable.Columns.Add("Name", typeof(string)); dataTable.Columns.Add("Description", typeof(string)); dataTable.Rows.Add(1, "Macaroni", "Tasty Pasta"); dataTable.Rows.Add(2, "Ramen", "Tasty Noodles"); dataTable.Rows.Add(3, "Figaro Oil", "Olive Oil"); dataTable.Rows.Add(4, "XYZ Lentils", "Nutritious Pulses"); return dataTable; } }
We then run a foreach loop on the data table and try to access all the rows:
var groceries = StaticDataRepository.GetGroceries(); for (var i = 0; i < groceries.Rows.Count; i++) { var row = groceries.Rows[i]; Console.WriteLine($"{row[0]}t {row[1]}t {row[2]}t {row[3]}"); }
The code raises an IndexOutOfRangeException
because we are trying to access the 4th column (row[3]
), which is not present in the data table:
Unhandled exception. System.IndexOutOfRangeException: Cannot find column 3.
It is quite possible that we might not be aware of the number of columns in the incoming data table.
We may try to access a column that does not exist and encounter the exception.
Solution
Let’s see how we can prevent the exception in this scenario:
var groceries = StaticDataRepository.GetGroceries(); foreach (DataRow grocery in groceries.Rows) { foreach (var item in grocery.ItemArray) { Console.WriteLine($"{item}"); } Console.WriteLine("********************"); }
We do not hardcode the indexes, rather we make use of the inbuilt properties such as ItemArray
that is an array of objects:
1 Macaroni Tasty Pasta ******************** 2 Ramen Tasty Noodles ******************** 3 Figaro Oil Olive Oil ******************** 4 XYZ Lentils Nutritious Pulses ********************
These were the most common situations where we can stumble upon the IndexOutOfRangeException
.
Conclusion
We have learned about IndexOutOfRangeException in detail, the various scenarios where the exception gets raised, and the possible solutions.
что это?
Это исключение означает, что вы пытаетесь получить доступ к элементу коллекции по индексу, используя недопустимый индекс. Индекс недействителен, если он ниже нижней границы коллекции или больше или равен количеству содержащихся в нем элементов.
Когда Его Бросают
Учитывая массив, объявленный как:
byte[] array = new byte[4];
Вы можете получить доступ к этому массиву от 0 до 3, значения за пределами этого диапазона приведут IndexOutOfRangeException
к выбрасыванию. Помните об этом при создании массива и доступе к нему.
Длина Массива
В C#, как правило, массивы основаны на 0. Это означает, что первый элемент имеет индекс 0, а последний элемент имеет индекс Length - 1
(где Length
общее количество элементов в массиве), поэтому этот код не работает:
array[array.Length] = 0;
Кроме того, пожалуйста, обратите внимание, что если у вас есть многомерный массив, то вы не можете использовать Array.Length
оба измерения, вы должны использовать Array.GetLength()
:
int[,] data = new int[10, 5];
for (int i=0; i < data.GetLength(0); i) {
for (int j=0; j < data.GetLength(1); j) {
data[i, j] = 1;
}
}
Верхняя Граница Не Включает
В следующем примере мы создаем необработанный двумерный массив Color
. Каждый элемент представляет собой пиксель, индексы-от (0, 0)
до (imageWidth - 1, imageHeight - 1)
.
Color[,] pixels = new Color[imageWidth, imageHeight];
for (int x = 0; x <= imageWidth; x) {
for (int y = 0; y <= imageHeight; y) {
pixels[x, y] = backgroundColor;
}
}
This code will then fail because array is 0-based and last (bottom-right) pixel in the image is pixels[imageWidth - 1, imageHeight - 1]
:
pixels[imageWidth, imageHeight] = Color.Black;
In another scenario you may get ArgumentOutOfRangeException
for this code (for example if you’re using GetPixel
method on a Bitmap
class).
Arrays Do Not Grow
An array is fast. Very fast in linear search compared to every other collection. It is because items are contiguous in memory so memory address can be calculated (and increment is just an addition). No need to follow a node list, simple math! You pay this with a limitation: they can’t grow, if you need more elements you need to reallocate that array (this may take a relatively long time if old items must be copied to a new block). You resize them with Array.Resize<T>()
, this example adds a new entry to an existing array:
Array.Resize(ref array, array.Length 1);
Don’t forget that valid indices are from 0
to Length - 1
. If you simply try to assign an item at Length
you’ll get IndexOutOfRangeException
(this behavior may confuse you if you think they may increase with a syntax similar to Insert
method of other collections).
Special Arrays With Custom Lower Bound
First item in arrays has always index 0. This is not always true because you can create an array with a custom lower bound:
var array = Array.CreateInstance(typeof(byte), new int[] { 4 }, new int[] { 1 });
In that example, array indices are valid from 1 to 4. Of course, upper bound cannot be changed.
Wrong Arguments
If you access an array using unvalidated arguments (from user input or from function user) you may get this error:
private static string[] RomanNumbers =
new string[] { "I", "II", "III", "IV", "V" };
public static string Romanize(int number)
{
return RomanNumbers[number];
}
Unexpected Results
This exception may be thrown for another reason too: by convention, many search functions will return -1 (nullables has been introduced with .NET 2.0 and anyway it’s also a well-known convention in use from many years) if they didn’t find anything. Let’s imagine you have an array of objects comparable with a string. You may think to write this code:
// Items comparable with a string
Console.WriteLine("First item equals to 'Debug' is '{0}'.",
myArray[Array.IndexOf(myArray, "Debug")]);
// Arbitrary objects
Console.WriteLine("First item equals to 'Debug' is '{0}'.",
myArray[Array.FindIndex(myArray, x => x.Type == "Debug")]);
This will fail if no items in myArray
will satisfy search condition because Array.IndexOf()
will return -1 and then array access will throw.
Next example is a naive example to calculate occurrences of a given set of numbers (knowing maximum number and returning an array where item at index 0 represents number 0, items at index 1 represents number 1 and so on):
static int[] CountOccurences(int maximum, IEnumerable<int> numbers) {
int[] result = new int[maximum 1]; // Includes 0
foreach (int number in numbers)
result[number];
return resu<
}
Конечно, это довольно ужасная реализация, но я хочу показать, что она не сработает для отрицательных чисел и чисел выше maximum
.
Как это относится к List<T>
?
Те же случаи, что и массив — диапазон допустимых индексов — 0 ( List
индексы всегда начинаются с 0) для list.Count
доступа к элементам за пределами этого диапазона, вызовут исключение.
Обратите внимание, что List<T>
броски ArgumentOutOfRangeException
выполняются для тех же случаев, когда используются массивы IndexOutOfRangeException
.
В отличие от массивов, List<T>
запускается пустым — поэтому попытка доступа к элементам только что созданного списка приводит к этому исключению.
var list = new List<int>();
Распространенным случаем является заполнение списка индексированием (аналогично Dictionary<int, T>
), что приведет к исключению:
list[0] = 42; // exception
list.Add(42); // correct
IDataReader и столбцы
Представьте, что вы пытаетесь прочитать данные из базы данных с помощью этого кода:
using (var connection = CreateConnection()) {
using (var command = connection.CreateCommand()) {
command.CommandText = "SELECT MyColumn1, MyColumn2 FROM MyTable";
using (var reader = command.ExecuteReader()) {
while (reader.Read()) {
ProcessData(reader.GetString(2)); // Throws!
}
}
}
}
GetString()
выбросит, IndexOutOfRangeException
потому что ваш набор данных содержит только два столбца, но вы пытаетесь получить значение из 3-го (индексы всегда основаны на 0).
Пожалуйста, обратите внимание, что это поведение является общим для большинства IDataReader
реализаций ( SqlDataReader
OleDbDataReader
и так далее).
Вы также можете получить такое же исключение, если используете перегрузку IDataReader оператора индексатора, который принимает имя столбца и передает недопустимое имя столбца.
Предположим, например, что вы получили столбец с именем Column1, но затем пытаетесь получить значение этого поля с помощью
var data = dr["Colum1"]; // Missing the n in Column1.
Это происходит потому, что оператор индексатора реализован при попытке получить индекс несуществующего поля Colum1. Метод GetOrdinal вызовет это исключение, когда его внутренний вспомогательный код вернет значение -1 в качестве индекса «Colum1».
Прочее
Существует еще один (задокументированный) случай, когда возникает это исключение: если DataView
имя столбца данных, указанное в DataViewSort
свойстве , недопустимо.
Как избежать
В этом примере позвольте мне для простоты предположить, что массивы всегда одномерны и основаны на 0. Если вы хотите быть строгим (или вы разрабатываете библиотеку), вам, возможно, потребуется заменить 0
на GetLowerBound(0)
и .Length
с GetUpperBound(0)
(конечно, если у вас есть параметры типа System.Arra
y, это не применимо T[]
). Пожалуйста, обратите внимание, что в этом случае верхняя граница включена, тогда этот код:
for (int i=0; i < array.Length; i) { }
Должно быть переписано вот так:
for (int i=array.GetLowerBound(0); i <= array.GetUpperBound(0); i) { }
Пожалуйста, обратите внимание, что это запрещено (это приведет InvalidCastException
к сбою), поэтому, если ваши параметры T[]
соответствуют, вы можете быть уверены в пользовательских массивах с нижней границей:
void foo<T>(T[] array) { }
void test() {
// This will throw InvalidCastException, cannot convert Int32[] to Int32[*]
foo((int)Array.CreateInstance(typeof(int), new int[] { 1 }, new int[] { 1 }));
}
Validate Parameters
If index comes from a parameter you should always validate them (throwing appropriate ArgumentException
or ArgumentOutOfRangeException
). In the next example, wrong parameters may cause IndexOutOfRangeException
, users of this function may expect this because they’re passing an array but it’s not always so obvious. I’d suggest to always validate parameters for public functions:
static void SetRange<T>(T[] array, int from, int length, Func<i, T> function)
{
if (from < 0 || from>= array.Length)
throw new ArgumentOutOfRangeException("from");
if (length < 0)
throw new ArgumentOutOfRangeException("length");
if (from length > array.Length)
throw new ArgumentException("...");
for (int i=from; i < from length; i)
array[i] = function(i);
}
If function is private you may simply replace if
logic with Debug.Assert()
:
Debug.Assert(from >= 0 amp;amp; from < array.Length);
Check Object State
Array index may not come directly from a parameter. It may be part of object state. In general is always a good practice to validate object state (by itself and with function parameters, if needed). You can use Debug.Assert()
, throw a proper exception (more descriptive about the problem) or handle that like in this example:
class Table {
public int SelectedIndex { get; set; }
public Row[] Rows { get; set; }
public Row SelectedRow {
get {
if (Rows == null)
throw new InvalidOperationException("...");
// No or wrong selection, here we just return null for
// this case (it may be the reason we use this property
// instead of direct access)
if (SelectedIndex < 0 || SelectedIndex >= Rows.Length)
return null;
return Rows[SelectedIndex];
}
}
Validate Return Values
In one of previous examples we directly used Array.IndexOf()
return value. If we know it may fail then it’s better to handle that case:
int index = myArray[Array.IndexOf(myArray, "Debug");
if (index != -1) { } else { }
How to Debug
In my opinion, most of the questions, here on SO, about this error can be simply avoided. The time you spend to write a proper question (with a small working example and a small explanation) could easily much more than the time you’ll need to debug your code. First of all, read this Eric Lippert’s blog post about debugging of small programs, I won’t repeat his words here but it’s absolutely a must read.
You have source code, you have exception message with a stack trace. Go there, pick right line number and you’ll see:
array[index] = newValue;
You found your error, check how index
increases. Is it right? Check how array is allocated, is coherent with how index
increases? Is it right according to your specifications? If you answer yes to all these questions, then you’ll find good help here on StackOverflow but please first check for that by yourself. You’ll save your own time!
A good start point is to always use assertions and to validate inputs. You may even want to use code contracts. When something went wrong and you can’t figure out what happens with a quick look at your code then you have to resort to an old friend: debugger. Just run your application in debug inside Visual Studio (or your favorite IDE), you’ll see exactly which line throws this exception, which array is involved and which index you’re trying to use. Really, 99% of the times you’ll solve it by yourself in a few minutes.
Если это произойдет в производстве, то вам лучше добавить утверждения в инкриминируемый код, вероятно, мы не увидим в вашем коде того, чего вы не видите сами (но вы всегда можете поспорить).
В VB.NET сторона этой истории
Все, что мы сказали в ответе на C#, справедливо для VB.NET с очевидными различиями в синтаксисе, но есть важный момент, который следует учитывать, когда вы имеете дело с VB.NET массивы.
В VB.NET, массивы объявляются, устанавливая максимальное допустимое значение индекса для массива. Это не количество элементов, которые мы хотим сохранить в массиве.
' declares an array with space for 5 integer
' 4 is the maximum valid index starting from 0 to 4
Dim myArray(4) as Integer
Таким образом, этот цикл заполнит массив 5 целыми числами, не вызывая исключения IndexOutOfRangeException
For i As Integer = 0 To 4
myArray(i) = i
Next
В VB.NET правило
Это исключение означает, что вы пытаетесь получить доступ к элементу коллекции по индексу, используя недопустимый индекс. Индекс недействителен, если он меньше нижней границы коллекции или больше, чем равно количеству содержащихся в нем элементов. максимально допустимый индекс, определенный в объявлении массива
The IndexOutOfRangeException
is an exception that will be thrown while accessing an element of a collection with an index that is outside of its range.
It occurs when an invalid index is used to access a member of a collection.
The following example throws the IndexOutOfRange
exception:
int[] arr = new int[5] { 10, 30, 25, 45, 65};
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);
Console.WriteLine(arr[3]);
Console.WriteLine(arr[4]);
Console.WriteLine(arr[5]); // throws IndexOutOfRange exception
In the above example, an arr
contains five elements. It will throw an IndexOutOfRange
exception when trying to access value more than its total elements.
Above, trying to access the 6th element using arr[5]
will throw IndexOutOfRange
exception.
Solutions to Prevent IndexOutOfRangeException
Solution 1: Get the total number of elements in a collection and then check the upper bound of a collection is one less than its number of elements.
The following example shows how to fix IndexOutOfRange
exception:
int[] arr = new int[5] { 10, 30, 25, 45, 65};
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);
Console.WriteLine(arr[3]);
Console.WriteLine(arr[4]);
if(arr.Length >= 6)
Console.WriteLine(arr[5]);
}
Solution 2: Use the try catch blocks to catche the IndexOutOfRangeException
.
static void Main()
{
try
{
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);
Console.WriteLine(arr[3]);
Console.WriteLine(arr[4]);
Console.WriteLine(arr[5]); // throws IndexOutOfRange exception
}
catch(Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
In the above example, the entire code wrapped inside the try block may throw errors.
The catch block has the Exception
filter that can catch any exceptions.
So, when the arr[5]
statement inside the try block throws an exception, the catch block captures the IndexOutOfRange
exception and displays an error message, and continues the execution.
Problem
A calculation fails with the «System.IndexOutOfRangeException: Index was outside the bounds of the array.» exception
Symptom
An inner exception will appear:
Varicent.Core.Exceptions.CalculationFailedException: Calculation failed at ‘example’ —> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at SparkService.CosDataReader.GetValueWithoutExtraQuotes(Int32 i)
at Varicent.Data.Calculations.SubTasks.Mathematical.ClassicComputer.ReadCurrentPartition(IDataReader rdr)
at Varicent.Data.Calculations.SubTasks.Mathematical.ClassicComputer.ComputeRows(IDataReader rdr)
at Varicent.Data.Calculations.SubTasks.Mathematical.SparkClassicComputer.ComputeCsvClassicCalcs(Boolean streamFromCos)
at Varicent.Data.Calculations.SubTasks.Mathematical.ClassicEvaluationSubTask.Output(ClassicQuery query, Boolean hasTrace, Int32 readCount, IProgressTracker tracker, IEvaluationContext context, ISqlStream cmds)
Cause
The calculation is unaggregated and it needs to follow best practices.
Diagnosing The Problem
Investigate the calculation that is in the error message by editing it. Go to the last step of the calculation wizard and check the formula.
Resolving The Problem
By adding a MAX(*) or SUM() operator to the formula and aggregating it, it should fix it.
Document Location
Worldwide
[{«Business Unit»:{«code»:»BU059″,»label»:»IBM Software w/o TPS»},»Product»:{«code»:»SSGKS6″,»label»:»Cognos Incentive Compensation Management»},»Component»:»»,»Platform»:[{«code»:»PF033″,»label»:»Windows»}],»Version»:»All Versions»,»Edition»:»»,»Line of Business»:{«code»:»LOB10″,»label»:»Data and AI»}}]
- Remove From My Forums
-
Question
-
User-501297529 posted
Get this error on a Submit button click event
How do I fix this?
protected void btnSumbit_Click(object sender, EventArgs e) { if (cboEmployees.SelectedIndex == 0 || cboForeman.SelectedIndex == 0 || cboOSA.SelectedIndex == 0) { lblInformation.Text = "Please make sure that all necessary fields are filled out and all hours entered are numeric"; return; } if (string.IsNullOrEmpty(txtDate.Text) || string.IsNullOrEmpty(txtJobNumber.Text) || string.IsNullOrEmpty(txtContractHours.Text) || string.IsNullOrEmpty(txtRainHours.Text) || string.IsNullOrEmpty(txtOtherHours.Text)) { lblInformation.Text = "Please make sure that all necessary fields are filled out and all hours entered are numeric"; return; } double d; if (!double.TryParse(txtContractHours.Text, out d) || !double.TryParse(txtOtherHours.Text, out d) || !double.TryParse(txtRainHours.Text, out d)) { lblInformation.Text = "Please make sure that all necessary fields are filled out and all hours entered are numeric"; return; } DateTime dt; if (!DateTime.TryParse(txtDate.Text, out dt)) { lblInformation.Text = "Please make sure that all necessary fields are filled out and all hours entered are numeric"; return; } string[] array = cboEmployees.Text.Split(','); EmployeeHours hours = new EmployeeHours(); hours.JobNumber = txtJobNumber.Text; hours.ContractHours = double.Parse(txtContractHours.Text); hours.MillerHours = double.Parse(txtRainHours.Text); hours.TravelHours = double.Parse(txtOtherHours.Text); hours.TimeCardDate = txtDate.Text; PayrollManager payrollManager = new PayrollManager(DateTime.MinValue, DateTime.MinValue, array[1], array[0]); //payrollManager.ProcessNewPayrollEntry(Int64.Parse(cboEmployees.SelectedValue), hours, cboOtherReasons.Text, txtNotes.Text, cboForeman.Text, cboOSA.Text, txtAltJobNumber.Text); GetFromViewStateToSaveToSession(); Response.Redirect("~/Payroll.aspx"); }
Answers
-
User753101303 posted
Hi,
Most often when an item is selected inside a drop down you want to process the «id» of the selected item, not the text shown to the user. So I would change later code to deal with an employee id rather than with an employee name.
Now the confusing part is that the Text property of a DropDown returns actually the Value (not the Text) for the selected item. This is because for most if not all web controls, the Text property is whatever is posted back to the web server (a dropdown renders
an HTML «select» tag, the value posted back to the server is the «value» attribute of the selected option:
https://www.w3schools.com/tags/tag_select.asp).-
Marked as answer by
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by
-
User475983607 posted
bootzilla
That line is what is causing the error but when I hover over .Text it shows the employee id which I posted a screen shot of in this thread. I can’t change that dropdown to show the id it has to show the last name, first name. Is there a way to get around
that error? There has to be. That’s what I need help with.Well, this line of code gets the selected text of a dropdownlist.
cboEmployees.Text
If the selected text is a number and you expect a «lastname, fistname» then there is a problem populating the dropdown.
If you are using a 3rd party control then you’ll need to consult the docs for the control as what you’re stated here is confusing. However, the id is what you want, IMHO, not the name. As stated above names are NOT unique.
Change the PayrollManager() method to take the ID as a parameter not the first and last name.
-
Marked as answer by
Anonymous
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by