3

Im doing this:

string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}};
int b = 0;

for(int i = 0; i <= string1.Length; i++)
{
     b = int.Parse(string1[i, 1]); // Error occurs here
}

Im getting an error saying that "index extent the limits of the array" (or something like that, the error is in danish).

2
  • 2
    WOW there are a lot of people posting the same incorrect answer for this one. Nicely asked question ;-) Commented Aug 29, 2012 at 12:57
  • 1
    i hope this question gets a "Popular Question" badge. It definitely deserves one. very interesting answers. Commented Aug 29, 2012 at 12:59

7 Answers 7

8

There are two problems:

  • string1.Length will return 6, as that's the total length of the array
  • You're using <= for the comparison, so it would actually try to iterate 7 times.

Your for loop should be:

for (int i = 0; i < string1.GetLength(0); i++)

The call to GetLength(0) here will return 3, as the size of the "first" dimension. A call to GetLength(1) return 2, as the second dimension is of size 2. (You don't need that though, as you're basically hard-coding the knowledge that you want the second "column" of each "row".)

See the docs for Array.GetLength() for more details.

Sign up to request clarification or add additional context in comments.

Comments

7

Your array bounds are incorrect in the loop:

for(int i = 0; i <= string1.Length; i++) 

should read:

for(int i = 0; i < string1.GetLength(0); i++) 

Two things were wrong: <= versus < meant you were going one item too far, and .Length returns the total length of the array (6) versus GetLength(0) which returns the length of the first dimension (3).

Comments

2

You already have the right answer, I'll update mine just to correct it. To right iteration should be

        for (int i = 0; i < string1.GetLength(0); i++)
        {
            b = int.Parse(string1[i, 1]);
        }

Because i stands for the length of the first dimension, and the fixed 1 will return the number, that is the second element.

I'm sorry for the wrong answer I gave first.

1 Comment

Doesn't work, because it is a two dimensional array. Check before posting!
0

Change i <= string1.Length to i < string1.Length.

Comments

0

change to

for(int i = 0; i <= string1.GetLength(0); i++) 
{ 
b = Int32.Parse(string1[i][0]);
}

Your code is refering at index 1 rather than 0 which causes exception known as Array out of bound

Comments

0

Try this:

for(int i = 0; i < string1.GetLength(0) ; i++)
{
     b = int.parse(string1[i, 0]); // Error occurs here
}

Array.GetLength Method

Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.

Comments

0

Also worth mentioning that int.Parse may throw a FormatException, consider int.TryParse.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.