2

I came across this strange IndexOutOfRangeException exception. The code I am working with was originally a C code, I have managed to translate it to C#

So I the followin loop gives me the error:

for (int i = 0; i < 6; i++)
{
    L[0] = new double[]{ T[0] + rxp[0][i] - (p[0][i])
}

According to the 'Locals' tab, rxp has only 3 'children' with only 1 items / child. So I guess the problem is here:

void getrxp()
{
    for (int i = 0; i < 6; i++)
    {
        rxp[0] = new double[]{ M[0][0] * (re[0][i]) + M[0][1] * (re[1][i]) + M[0][2] * 0};
        rxp[1] = new double[]{ M[1][0] * (re[0][i]) + M[1][1] * (re[1][i]) + M[1][2] * 0};
        rxp[2] = new double[]{ M[2][0] * (re[0][i]) + M[2][1] * (re[1][i]) + M[2][2] * 0};
    }
}

Am I getting this to to create a double rxp[3][6]; array wrong or there is something else?

The original code looks like this (C):

void getrxp()
{
  for(int i=0;i<6;i++){
    rxp[0][i] = M[0][0]*(re[0][i])+M[0][1]*(re[1][i])+M[0][2]*0;
    rxp[1][i] = M[1][0]*(re[0][i])+M[1][1]*(re[1][i])+M[1][2]*0;
    rxp[2][i] = M[2][0]*(re[0][i])+M[2][1]*(re[1][i])+M[2][2]*0;  
  }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Zoszko
  • 75
  • 1
  • 7
  • 1
    Why would you think `rxp[0][i]` in C is equivalent to `rxp[0]` in C#? – Amit Feb 13 '16 at 03:59
  • According to the C function that he's trying to convert to c# to, he's not trying to create an array of arrays. So, duplicate mark is wrong. – sithereal Feb 13 '16 at 04:05
  • Well this if you take a look at Jagged Arrays at MSDN, you can see why I've made it like this. – Zoszko Feb 13 '16 at 04:05

1 Answers1

0

If you are trying to create a 2-dimensional array, the syntax should be double [,] rxp = new double[3,6];. Here's the detailed documentation of Multidimensional Arrays at MSDN.

sithereal
  • 1,656
  • 9
  • 16