Hello, is it possible to alter the following code under the generating pattern of the Factory Method, and if it is possible how:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace testNasledovanie { class Vector { public int[] arr; private const int size = 4; public virtual void Input() { Console.WriteLine("Initialzing"); arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = Int32.Parse(Console.ReadLine()); } } public virtual void Output() { Console.WriteLine("Your vector"); for (int i = 0; i < size; i++) { Console.WriteLine(arr[i] + " "); } } public virtual int Ymova() { int max = 0; for (int i = 0; i < size; i++) { if (arr[i] > max) max = arr[i]; } return max; } ~Vector() { Console.WriteLine("destructor vector is called."); Console.ReadKey(); } } class Maxtix : Vector { public int[,] array; private const int size = 4; public override void Input() { array = new int[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { array[i, j] = Int32.Parse(Console.ReadLine()); } } } public override void Output() { Console.WriteLine("Your 2D array"); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Console.Write(array[i, j] + " "); } Console.WriteLine(); } } public override int Ymova() { int max = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (array[i, j] > max) max = array[i, j]; } } return max; } ~Maxtix() { Console.WriteLine("destructor matrix is called."); Console.ReadKey(); } } class Program { static void Main(string[] args) { int userSelect; Vector baseobj = new Vector(); do { Console.WriteLine("Enter '0' if you want to work with vector and 1 with matrix"); userSelect = Convert.ToInt32(Console.ReadLine()); if (userSelect == 0) { baseobj = new Vector(); baseobj.Input(); Console.WriteLine("max is " + baseobj.Ymova()); } else if (userSelect == 1) { baseobj = new Maxtix(); baseobj.Input(); Console.WriteLine("MAX IS " + baseobj.Ymova()); } else { return; } baseobj.Output(); } while (true); } } } 

This code allows you to create objects of classes of vector or matrix (there is a small menu). Thank you in advance!

  • Console.ReadKey(); in the finalizer? o_O - VladD
  • I made a delay to show that the destructor is working, but what about my question, is it possible to do this at all? - M-Misha-M

1 answer 1

Your code is terrible. For example, the complete lack of encapsulation, as well as non-segregation of duties. The vector should not (no, not so: MUST NOT!) Be engaged in reading data from the keyboard.

Divide it into parts:

 class Vector { private const int size = 4; private int[] arr = new int[4]; Vector(int a, int b, int c, int d) { arr[0] = a; arr[1] = b; arr[2] = c; arr[3] = d; } // input - не дело вектора, перенесли // называть функции транслитом - отвратительно // никогда больше так не делайте public virtual int Max() { // инициализировать надо не нулём, а минимальным возможным значением int max = int.MinValue; for (int i = 0; i < size; i++) { if (arr[i] > max) max = arr[i]; } return max; } // деструктору не место в массиве, у вас нету неуправляемых данных // (и вы наверное не знаете, что это такое) } static class VectorFactory { public static Vector InputFromConsole() { Console.WriteLine("Enter vector values (one per line):"); int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int c = int.Parse(Console.ReadLine()); int d = int.Parse(Console.ReadLine()); return new Vector(a, b, c, d); } } 
  • Thanks, I know that “bydlokod”, for example, the destructor was demanded by the teacher, it translates it, yes, it’s terrible that your hands didn’t get to this program, but look in your code only for vector implemented or what? because I still have a matrix there. And unmanaged data is the data that I understand is not used in the program? - M-Misha-M
  • @ M-Misha-M: It's bad that the teacher demanded a destructor. So he does not understand the language at all. Learn from other sources. I have implemented only for the vector, with the matrix in theory the same. - VladD
  • @ M-Misha-M: Unmanaged data is data that is not managed by a .NET memory management system. need to work hard to get such. In short, do not worry, this has nothing to do with your problem. - VladD