Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through other part of dimension in a for loop throws an IndexOutOfRangeException (C#)

Tags:

c#

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.

like image 220
Everblack Avatar asked Apr 13 '16 02:04

Everblack


People also ask

How do I fix IndexOutOfRangeException in C#?

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 .

What is IndexOutOfRangeException in C sharp?

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.

What is GetLength C#?

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.

How do you solve an index outside the bounds of the array?

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 !


2 Answers

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].

like image 160
Grant Winney Avatar answered Sep 28 '22 23:09

Grant Winney


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++;
     }
}
like image 37
AGB Avatar answered Sep 28 '22 21:09

AGB