Вываливается EXEPTION при перегрузке оператора. Порядок действий внизу - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication_Si_Shar_lb_2__part_2_
{
class StDemoArray
{
int[,] MyArray;
int n, m;//закрытые поля: размерность массива
public StDemoArray(int sizeN, int sizeM)//konstructor
{
MyArray = new int[sizeN, sizeM];
this.n = sizeN;
this.m = sizeM;
}
//======================= конструктор для перегрузки метода для умножения массива на массив
public StDemoArray()//konstructor
{
MyArray = new int[this.n, this.m];
}
//public StDemoArray()//konstructor
//{
// this.n = n;
// this.m = m;
// MyArray = new int[n, m];
//}
public int LengthN //свойство, возвращающее количество строк
{
get { return n; }
}
public int LengthM //свойство, возвращающее количество столбцов
{
get { return m; }
}
//============================================================= //индексатор
public int this[int i, int j] //индексатор
{
get
{
if (i < 0 || i >= n || j < 0 || j >= m) throw new Exception("выход за границы массива");//---------ОШИБКА ЗДЕСЬ !!!!!!!!
else return MyArray[i, j];
}
set
{
if (i < 0 || i >= n || j < 0 || j >= m) throw new Exception("выход за границы массива");
else if (value >= 0 && value <= 100) MyArray[i, j] = value;
else throw new Exception("присваивается недопустимое значение");
}
}
public static StDemoArray operator ++(StDemoArray x)//заголовков унарных операций
{
for (int j = 0; j < x.m; j++)
{
for (int i = 0; i < x.n; i++)
{
x[i, j] = x[i, j] + 1;
}
}
return x;
}
public static StDemoArray operator --(StDemoArray x)//заголовок унарных операций
{
for (int j = 0; j < x.m; j++)
{
for (int i = 0; i < x.n; i++)
{
x[i, j] = x[i, j] - 1;
}
}
return x;
}
public int LengthArray //свойство, возвращающее размерность
{
get { return MyArray.Length; }
}
public void Print(string name) //метод - выводит поле-массив на экран
{
Console.WriteLine(name + ": ");
for (int i = 0; i < MyArray.Length; i++)
for (int j = 0; i < MyArray.Length; j++)
Console.Write(MyArray[i,j] + " ");
Console.WriteLine();
}
public void Print2()
{
for (int i = 0; i < this.n; i++, Console.WriteLine())
for (int j = 0; j < this.m; j++)
Console.Write("{0,1} ", this.MyArray[i, j]);
}
public static bool operator true(StDemoArray a) //перегрузка константы true
{
foreach (int i in a.MyArray)
{
if (i < 0)
{
return false;
}
}
return true;
}
public static bool operator false(StDemoArray a)//перегрузка константы false
{
foreach (int i in a.MyArray)
{
if (i > 0)
{
return true;
}
}
return false;
}
//==============================================>>> перемножить два массива <<< ======================
public static StDemoArray operator * (StDemoArray x, StDemoArray y)//заголовков унарных операций
{
//StDemoArray temp = new StDemoArray();
//if (x.LengthArray == y.LengthArray)
//{
for (int i = 0; i < x.LengthArray; ++i)
{
for (int j = 0; j < x.LengthArray; ++j)
{
x[i, j] = x[i, j] * y[i, j];
//return x;
}
}
//}
//else
//{
// throw new Exception("несоответствие размерностей");
//}
return x;
}
}
//============================================================= //переопределение
class Program
{
static void Main()
{
Console.WriteLine("Введите размерность массива");
Console.Write(" Массив размерностью n * m :");
Console.Write("\n n=");
int x = int.Parse(Console.ReadLine());
Console.Write(" m=");
int y = int.Parse(Console.ReadLine());
StDemoArray a = new StDemoArray(x,y);
for (int i = 0; i < a.LengthN; i++, Console.WriteLine())
{
for (int j = 0; j < a.LengthM; j++)
{
a[i, j] = i * j; // использование индексатора в режиме записи
Console.Write("{0,5}", a[i, j]);// использование индексатора в режиме чтения
}
}
Console.WriteLine();
try
{
//Console.WriteLine(a[3, 3]);
//a[0, 0] = 200;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("применим перегрузку оператора ++");
++a;
a.Print2();
Console.WriteLine();
Console.WriteLine("применим перегрузку оператора --");
Console.ReadLine();
--a;
a.Print2();
Console.ReadLine();
if (a)
{
Console.WriteLine("\nВ массиве все элементы положительные\n");
}
else Console.WriteLine("\nВ массиве есть не положительные элементы\n");
Console.ReadLine();
Console.WriteLine("создадим второй массив, такой же размерности");
StDemoArray b = new StDemoArray(x, y);
for (int i = 0; i < b.LengthN; i++, Console.WriteLine())
{
for (int j = 0; j < b.LengthM; j++)
{
b[i, j] = i * j; // использование индексатора в режиме записи
Console.Write("{0,5}", b[i, j]);// использование индексатора в режиме чтения
}
}
Console.WriteLine();
try
{
//Console.WriteLine(a[3, 3]);
//a[0, 0] = 200;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("перемножим 2 массива соответствующих размерностей");
StDemoArray c = new StDemoArray(x,y);
c = a*b;
c.Print2();
Console.ReadLine();
}
}
}Решение задачи: «Вываливается EXEPTION при перегрузке оператора. Порядок действий внизу»
textual
Листинг программы
/// <summary>Свойство, возвращающее количество строк</summary>
public int LengthN //
{
get { return MyArray.GetLength(0); }
}
/// <summary>Свойство, возвращающее количество столбцов</summary>
public int LengthM
{
get { return MyArray.GetLength(1); }
}