There is a certain class with a constructor.

public class Neuron { public string name { get; set; } public string rusname { get; set; } public float[,] weight { get; set; } public float sum {get;set;} public Neuron(string name,string rusname) { this.name = name; this.rusname = rusname; weight = new float[7, 7]; sum = 0; } } 

In another class, I create objects

 neurons[0] = new Neuron("Zero", "Ноль"); neurons[1] = new Neuron("One", "Один"); neurons[2] = new Neuron("Two", "Два"); neurons[3] = new Neuron("Three", "Три"); neurons[4] = new Neuron("Four", "Четыре"); neurons[5] = new Neuron("Five", "Пять"); neurons[6] = new Neuron("Six", "Шесть"); neurons[7] = new Neuron("Seven", "Семь"); neurons[8] = new Neuron("Eight", "Восемь"); neurons[9] = new Neuron("Nine","Девять"); 

At the end of the program, I try to display the object names.

 string str = neurons[9].name; string str1 = neurons[9].rusname; 

In this case, the first line has a value and there are no problems with the output, and the value of the second one is null. For what reason?

Name output method:

 public void screen_info() { terminal.Clear(); for(int i = 0; i < neurons.Length; i++) { terminal.Text += neurons[i].name + ": " + neurons[i].sum + Environment.NewLine+Environment.NewLine; } string str = neurons[9].name; string str1 = neurons[9].rusname; terminal.Text += str + " " + str1; } 

Found a piece of code, after which the value of the second name becomes null

  if (File.Exists("weights.txt")) { using(FileStream file = new FileStream("weights.txt", FileMode.Open)) { neurons = (Neuron[]) upload_saves.Deserialize(file); } } 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

 public string name { get; set; } public string rusname { get; set; } public float[,] weight { get; set; } public float sum {get;set;} 

So that the value does not change anywhere except the constructor, it is necessary to replace it with

 public string name { get; } public string rusname { get; } public float[,] weight { get; } public float sum { get; } 
 public Neuron(string name,string rusname) { this.name = name; this.rusname = rusname; 

And here you need to make sure that the argument name is not a typo. It is better to take the name of the property and copy it into all 3 places.

  • No, it does not help, look, I added the code - ZOOM SMASH
  • In sum, then it’s really necessary to set the private set :) - Grundy
  • @Grundy, did not understand. - Qwertiy
  • @Qwertiy, oh! I didn’t look there :-) but now it seems like the sum can only be assigned once in the constructor? and he wanted to count something in it :) - Grundy
  • @Grundy, well, fix the bug, then calculate) - Qwertiy