Good day. One of the buttons has an event (creating an array of queues, the number of elements in which is output to the labels), which includes the following code block:

{ Random rnd = new Random(); M = int.Parse(textBox1.Text); //Количество очередей QArray = new Queue<int>[M]; int [] Array = new int[30]; for (int i = 0; i < 30; i++) { Array[i] = i + 1; } int ArrayCount = 30; int MaximumValue; int element; for (int i = 0; i < M; i++) { { MaximumValue = 0; QArray[i] = new Queue<int>(); int count = rnd.Next(1, 8); //Количество элементов в очереди for (int j = 0; j < count; j++) { if (ArrayCount > 0) { if (MaximumValue == 30) break; element = rnd.Next(0, 30); do { element = rnd.Next(0, 30); if (Array[element] < MaximumValue) continue; } while (Array[element] == 0); MaximumValue = Array[element]; Array[element] = 0; QArray[i].Enqueue(Array[element]); ArrayCount--; } } } if (MaximumValue > MaximumofM) MaximumofM = MaximumValue; labelArray = new Label[M]; labelArray[i] = new Label(); labelArray[i].Location = new Point(30 + 40 * i, 100); labelArray[i].Text = QArray[i].Count.ToString(); labelArray[i].Size = new Size(25, 15); this.Controls.Add(labelArray[i]); } } 

By pressing another button in one of the array queues, an element is added, so the value on the labels should change. The code below would have to remove all labels, but they do not change. Tell me how to change the values ​​in the labels now? Thank you in advance

 int add=rnd.Next(0, M+1); QArray[add].Enqueue(MaximumofM); for (int i=0;i< M; i++) { labelArray[i] = new Label(); labelArray[i].Text = QArray[i].Count.ToString(); //Количество элементов в очереди labelArray[i].Location = new Point(30 + 40 * i, 100); labelArray[i].Size = new Size(25, 15); } 
  • I read your question twice, but I could not understand it. There is something to supplement it to make it clearer? Maybe add screenshots? - Denis Bubnov
  • @DenisBubnov When you click on the add item to the queue, the queue data should change (the number of items in one should be greater) dynamically, that is The text in the labels should change, but this does not happen, how to achieve this? - Maxim Ustelemov
  • labelArray[i] = new Label(); - remove this line from the second code fragment - Igor
  • @DenisBubnov Then in this place the exception will appear "Object link does not indicate an object instance." - Maxim Ustelemov
  • @Igor offers to remove the line :) but I, like him, also think that it is superfluous. Since this is still a new object, not an existing old one. - Denis Bubnov

1 answer 1

 int add=rnd.Next(0, M+1); QArray[add].Enqueue(MaximumofM); labelArray[add].Text = QArray[add].Count.ToString(); // цикл не нужен 
  • With this code, an exception is thrown: "The link to the object does not indicate an instance of the object.". - Maxim Ustelemov
  • Learn to use the debugger. The exception on which line, what values ​​of the variables used in the code of this line? - Igor
  • Understood, thanks. - Maxim Ustelemov