0

I am new to C#. I am trying to make a matrix calculator but first I need the user to give me the size of and the contents of said matrix. I have 2 classes.

First class is as follows:

class Assignment1
{
    static void Main(string[] args)
    {
        Console.Write("Please enter the number of rows in the matrix: ");
        int row = int.Parse(Console.ReadLine());

        Console.Write("Please enter the number of columns in the matrix: ");
        int columns = int.Parse(Console.ReadLine());

        MatrixN matrix =new MatrixN(row, columns);

        int i = 0;
        for (double x = 0; x < row; x++)
        {
            for(double y = 0; y < columns; y++)
            {

                if(i == 0)
                {
                    Console.Write("Enter first value of the matrix: ");
                    matrix[x, y] = double.Parse(Console.ReadLine());
                    i++;
                }
                else if (i == row * columns)
                {
                    Console.Write("Enter last value of the matrix: ");
                    matrix[x, y] = double.Parse(Console.ReadLine());
                    i++;
                }
                Console.Write("Enter nest value of the matrix: ");
                matrix[x, y] = double.Parse(Console.ReadLine());
                i++;
            }
        }
    }
}

Second class is:

 class MatrixN
{

    double[,] m;

    public MatrixN(int row, int column)
    {
        m = new double[row, column];
    }

I keep getting the error: cannot apply indexing with [] to an expression of MatrixN for the code

matrix[x, y] = double.Parse(Console.ReadLine());

Any help would be greatly appreciated. Thank you.

0

1 Answer 1

1

matrix is a variable of type MatrixN. The type MatrixN does not support the subscription/indexing operation, so you cannot access the value matrix[i, j], nor set the value matrix[i, j] = v;.

You can either expose the m member variable of the MatrixN class (by making it public), and set/access the value of matrix.m, or write your own index operator:

class MatrixN {
   private double[,] inner;
   public double this[int x, int y] {
       get { return inner[x, y]; }
       set { inner[x, y] = value; }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.