Hello, I just started to learn C #, I encountered such a problem.

It is necessary to initialize the array and its size from the keyboard, and then open access to it and its elements for another class.

Non-working code:

namespace ConsoleApplication1 { class MainClass { static void Main() { Massive instance = new Massive(); Console.WriteLine("Π Π°Π·ΠΌΠ΅Ρ€ массива: "); instance.Size = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(instance.Size); instance.Input(); instance.Output(); Console.ReadKey(); } } class Massive { int i; int size = 0; int []mass = new int[size]; //ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€ поля Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΎΠ±Ρ€Π°Ρ‰Π°Ρ‚ΡŒΡΡ ΠΊ нСстатичСскому полю public int Size { set { size = value; } get { return size; } } public void Input() { for (i = 0; i < size; i++) { Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт массива: "); mass[i] = Convert.ToInt32(Console.ReadLine()); } } public void Output() { for (i = 0; i < size; i++) { Console.Write(" "+mass[i]); } } } class Massive2 : Massive { //доступ ΠΊ массиву ΠΈ Π΅Π³ΠΎ элСмСнтам с класса Massive } } 

Updated code

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class MainClass { static void Main() { Massive instance = new Massive(); Console.WriteLine("Π Π°Π·ΠΌΠ΅Ρ€ массива: "); instance.Size = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(instance.Size); instance.Input(); instance.Output(); Massive2 instance2 = new Massive2(); Console.WriteLine(); instance2.Method(); Console.ReadKey(); } } class Massive { int[] mass = null; int i; public int Size { set { if (value > 0) mass = new int[value]; else mass = null; } get { return (mass != null) ? mass.Length : 0; } } public int this[int index] { get { return mass[index]; } //ошибка здСсь set { mass[index] = value; } } public void Input() { for (int i = 0; i < Size; i++) { Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт массива: "); mass[i] = Convert.ToInt32(Console.ReadLine()); } } public void Output() { for (i = 0; i < Size; i++) { Console.Write(" " + mass[i]); } } } class Massive2 : Massive { public void Method() { //доступ ΠΊ массиву ΠΈ Π΅Π³ΠΎ элСмСнтам с класса Massive int test = this[4]; Console.WriteLine(test); } } 

}

  • so, instance2 is a completely different object, where Size is zero and mass is null. Add the line instance2.Size = 5; before instance2.Method(); - Igor

1 answer 1

 class Massive { int i; int size = 0; int []mass = null; public int Size { set { size = value; // check for size > 0 mass = new int[size]; } get { return size; } } public int this[int index] { get { return mass[index]; } set { mass[index] = value } } ... 

You can do without the size field, and use the array itself to get its length:

 class Massive { int []mass = null; public int Size { set { if (value > 0) mass = new int[value]; else mass = null; } get { return (mass != null)? mass.Length : 0; } } public int this[int index] { get { return mass[index]; } set { mass[index] = value } } public void Input() { for (int i = 0; i < Size; i++) { Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт массива: "); mass[i] = Convert.ToInt32(Console.ReadLine()); } } ... 

Update

 static void Main() { Massive2 instance2 = new Massive2(); Console.WriteLine("Π Π°Π·ΠΌΠ΅Ρ€ массива: "); instance2.Size = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(instance2.Size); instance2.Input(); instance2.Output(); instance2.Method(); Console.ReadKey(); } 
  • Thank! And how do you get access to some element of mass in the class Massive2: Massive? I try int x = this.mass [4]; but I get the error "Object link does not indicate an object instance." - hjdds
  • @mwchqq int x = this[4]; , well, make sure that before that you have appointed a Size at least five - Igor
  • I can not understand why it is impossible, Size is not less than five. i.imgur.com/j5MXZAT.png - hjdds
  • @mwchqq In the picture can not be said. It is obvious that a member of the mass class is null . Add the full code (for now) to the question. The error should be simple. - Igor
  • @mwchqq so, instance2 is a completely different object, for which Size is zero and mass is null. Add the line instance2.Size = 5; before instance2.Method(); - Igor