Jump to content

  • Log In with Google      Sign In   
  • Create Account

Why am I getting this IndexOutOfRangeException?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 06 May 2013 - 09:07 PM

Hi, this is my code.
public Matrix GetCol (int col)
{
  if (col > this.Cols)
  {
    throw new System.Exception("Can't get column #" + col
      + " because it's greater than the number of columns " + this.Cols
      + ".");
  }

  double [,] newMatrix = new double[this.Rows, 1];

  for (int row = 0; 0 < this.Rows; row++)
  {
    newMatrix[row, 0] =  // HERE!!!!
      this.matrix[row, col];
  }

  return new Matrix(newMatrix);
}
I traced my code and on the line indicated, I get an System.IndexOutOfRangeException. This... makes zero sense. This is a single-threaded application and this.Rows is not being modified. So why is there an exception in there?

Edited by ysg, 06 May 2013 - 09:09 PM.


Sponsor:

#2 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 06 May 2013 - 09:16 PM

The weird thing, is that while I'm tracing the code, when row is being incremented and it becomes 4, it _still_ goes into the loop and then tries to go to newMatrix[4, 0], which makes sense that I'm getting the exception.

But what doesn't make ANY sense is the fact that it's getting into the loop body after row is equal to 4. At that point, the loop should have stopped long ago.

[edit]

Never mind, 0 < this.Rows is just stupid smile.png .

Edited by ysg, 06 May 2013 - 09:25 PM.


#3 Nypyren   Crossbones+   -  Reputation: 4798

Like
3Likes
Like

Posted 06 May 2013 - 09:21 PM

0 < this.Rows

 

row < this.Rows


Edited by Nypyren, 06 May 2013 - 09:23 PM.


#4 Dave Hunt   Crossbones+   -  Reputation: 2478

Like
1Likes
Like

Posted 06 May 2013 - 09:24 PM

Um... "0 < this.Rows"??? That should be "row < this.Rows".

 

edit -- tooo slooooow


Edited by Dave Hunt, 06 May 2013 - 09:25 PM.


#5 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 06 May 2013 - 09:26 PM

Um... "0 < this.Rows"??? That should be "row < this.Rows".
 
edit -- tooo slooooow

Yeah smile.png .

#6 evolutional   Moderators   -  Reputation: 1069

Like
1Likes
Like

Posted 07 May 2013 - 01:53 PM

Also, if you're using zero-based arrays then you need to check that col >= this.cols at the top, as if cols = 1, the max offset you can use is actually zero.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS