Cannot apply indexing with []

O

O.B.

I have the following:

public class Matrix3D
{
private const int SIZE = 3;
public double[,] matrix = new double[SIZE, SIZE];

public static Matrix3D operator *(Matrix3D lhs, Matrix3D rhs)
{
Matrix3D result = new Matrix3D();
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
for (int k = 0; i < SIZE; k++)
{
result[i, j] += lhs[i, k] * rhs[k, j];
}
}
}
return result;
}
}

When I compile, I get the error:
Cannot apply indexing with [] to an expression of type 'Matrix3D'

Help?
 
B

Brian Gideon

I have the following:

public class Matrix3D
{
private const int SIZE = 3;
public double[,] matrix = new double[SIZE, SIZE];

public static Matrix3D operator *(Matrix3D lhs, Matrix3D rhs)
{
Matrix3D result = new Matrix3D();
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
for (int k = 0; i < SIZE; k++)
{
result[i, j] += lhs[i, k] * rhs[k, j];
}
}
}
return result;
}

}

When I compile, I get the error:
Cannot apply indexing with [] to an expression of type 'Matrix3D'

Help?

Since result, lhs, and rhs are of type Matrix3D the compiler is
looking for an indexer which probably doesn't exist. You can either
add an indexer to Matrix3D or access the matrix field directly by
using the syntax lhs.matrix[x,y].

By the way, it's best practice to declare the matrix field as private.
 
J

Jon Skeet [C# MVP]

O.B. said:
I have the following:

public class Matrix3D
{
private const int SIZE = 3;
public double[,] matrix = new double[SIZE, SIZE];

public static Matrix3D operator *(Matrix3D lhs, Matrix3D rhs)
{
Matrix3D result = new Matrix3D();
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
for (int k = 0; i < SIZE; k++)
{
result[i, j] += lhs[i, k] * rhs[k, j];
}
}
}
return result;
}
}

When I compile, I get the error:
Cannot apply indexing with [] to an expression of type 'Matrix3D'

Help?

Well, you haven't defined an indexer. Presumably you meant it to use
the "matrix" member, but there's nothing you've specified to let the
compiler know that.

Either use

result.matrix[i, j] += lhs.matrix[i, k] * rhs.matrix[k, j];

or define your own indexer in the class.
 
O

O.B.

I have the following:
public class Matrix3D
{
private const int SIZE = 3;
public double[,] matrix = new double[SIZE, SIZE];
public static Matrix3D operator *(Matrix3D lhs, Matrix3D rhs)
{
Matrix3D result = new Matrix3D();
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
for (int k = 0; i < SIZE; k++)
{
result[i, j] += lhs[i, k] * rhs[k, j];
}
}
}
return result;
}

When I compile, I get the error:
Cannot apply indexing with [] to an expression of type 'Matrix3D'

Since result, lhs, and rhs are of type Matrix3D the compiler is
looking for an indexer which probably doesn't exist. You can either
add an indexer to Matrix3D or access the matrix field directly by
using the syntax lhs.matrix[x,y].

By the way, it's best practice to declare the matrix field as private.

Doh. I can't believe I missed such a simple error. Thank you!
 
N

Nicholas Paldino [.NET/C# MVP]

You have to replace the calls to result[i, j], lhs[i, k] and rhs[k, j]
with result.matrix[i, j], lhs.matrix[i, k] and rhs.matrix[k, j]
respectively.

If you wanted the matrix class to expose the matrix elements as part of
the indexer, then you have to declare it, like so:

public double this[int row, int column]
{
get
{
// Just return the value.
return matrix[row, column];
}

set
{
// Just set the value.
matrix[row, column] = value;
}
}

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top