There is a class representing an array. It is necessary for it to overload the operator of the remainder of division so that the output will be an array of the residuals from dividing each of the elements of the array by the specified number.

Abbreviated version of the code displays errors:

  1. A non-static field, method, or property "Array.length" requires an object reference.
  2. Cannot apply indexing via [] to an expression of type "Array"
namespace array { class Array { public bool errFlag; int [,] arr; int length; public Array(int length) { this.length = length; arr = new int[length, length]; Console.WriteLine(); } public static Array operator %(Array ob1, int ob2) { for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { ob1[i, j] = ob1[i, j] % ob2; } } return ob1; } public void SetArr() { Console.WriteLine("Заполните массив данными"); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { Console.Write("Введите значение элемента массива {0}-{1}: ", i + 1, j + 1); arr[i, j] = int.Parse(Console.ReadLine()); } } } public void GetArr() { Console.WriteLine("Значения элементов массива:"); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { Console.Write(arr[i, j] + "\t"); } Console.WriteLine(); } } } class Program { static void Main(string[] args) { Console.Write("Введите размер массивов с которыми предстоит работать: "); int length = int.Parse(Console.ReadLine()); Array firstArr = new Array(length); firstArr.SetArr(); firstArr.GetArr(); Console.Write("Введите число, с помощью которого будут найдены остатки от деления всех элементов массива : "); int x = int.Parse(Console.ReadLine()); firstArr = firstArr % x; Console.WriteLine("Новый массив, который состоит из остатков от деления"); firstArr.GetArr(); } } } 
  • one
    Everything is logical, the operators are static, and you need to get the length from the 1st ob1 , but not try to pull it out of the class. In general, attach the entire class, it is not clear what the Array . - LLENN
  • pastebin.com/TU1vBGdZ - Frank Matrix
  • Can it be about overloading , not rebooting ? - MrModest
  • You are in the static public static Array operator %(Array ob1, int ob2) method public static Array operator %(Array ob1, int ob2) trying to use the length field of the instance, so you get errors - tym32167

1 answer 1

In order to apply indexing to a class (that is, square brackets like aki in the default array), you first need to overload the indexing operator:
public int this[int index] or something like that ..

In overloading the remainder of the division, you manipulate the same array, that is, change it. It doesn't look very good. It seems to me that it is better to create a new array with the result, and not to change the existing one.