I'm having an issue when iterating through one dimension of a two dimensional array, in a C# console application. It's part of a game that comes up with witty responses for each time you miss a shot, or you shot successfully. Let me start off with a two-dimensional boolean that I've made:
public static bool[,] hasResponseBeenUsedBefore = new bool[2, 11];
There are two rows in the first dimension. 1 is to accompany responses where shots are successful. And 2 is to accompany responses where a shot is missed.
In a method that I've created to generate a response, I attempt to iterate through the second dimension.
int usedManyTimes = 0;
for (int i = 0; i < hasResponseBeenUsedBefore.GetLength(1); i++)
{
MessageBox.Show(i.ToString());
if (hasResponseBeenUsedBefore[2, i] == true) // 2 is the dimension for unsuccessful responses
{
usedManyTimes++;
}
}
I've tried to get the length of the second dimension with no success. It throws out an IndexOutOfRangeException
, with the following information:
HResult: -2146233080
Exception message: Index was outside the bounds of the array.
Any help on this would be greatly appreciated. Thanks for your time.
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. Solution 2: Use the try catch blocks to catche the IndexOutOfRangeException .
An IndexOutOfRangeException exception is thrown when an invalid index is used to access a member of an array or a collection, or to read or write from a particular location in a buffer. This exception inherits from the Exception class but adds no unique members.
GetLength(Int32) Method is used to find the total number of elements present in the specified dimension of the Array. Syntax: public int GetLength (int dimension); Here, dimension is a zero-based dimension of the Array whose length needs to be determined. Return value: The return type of this method is System.
U are accessing an empty array with some index greater than zero, it returns this error. Solution : Check the array count before you access it through index !
To get the "unsuccessful" dimension, use 1
, not 2
:
if (hasResponseBeenUsedBefore[1, i] == true)
Arrays use zero-based indexes. When you define an array like:
var hasResponseBeenUsedBefore = new bool[2, 11];
you access its elements using hasResponseBeenUsedBefore[0][0]
through hasResponseBeenUsedBefore[1][10]
.
Arrays use zero-based indexing. The array in the first dimension has size 2, therefore it only has indexes 0 and 1 available. The second dimension arrays have size 11, so they have indexes 0 through (and including) 10 available.
Try
int usedManyTimes = 0;
for (int i = 0; i < hasResponseBeenUsedBefore.GetLength(1); i++)
{
MessageBox.Show(i.ToString());
if (hasResponseBeenUsedBefore[1, i] == true)//notice the change from [2,i] to [1,i] here
{
usedManyTimes++;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With