Как получить переменную из массива объектов? - C#

Узнай цену своей работы

Формулировка задачи:

Создал массив объектов. В массив запихнул [TextBox, double]. Далее перебираю в цикле элементы массива. У меня не получается в глобальные переменные (my_class.variables.main_plate_width... ) передать значение. Как это сделать?
Листинг программы
  1. public static bool calculate()
  2. {
  3. bool further;
  4. object[,] text_box_and_variables = new object[,]
  5. {
  6. { my_class.text_box.main_plate_width, my_class.variables.main_plate_width },
  7. { my_class.text_box.main_plate_length, my_class.variables.main_plate_length },
  8. { my_class.text_box.main_plate_height, my_class.variables.main_plate_height }
  9. };
  10. for (int i = 0; i < 2; i++)
  11. {
  12. double a = (double)text_box_and_variables[i, 1]; // здесь наверное проблема
  13. TextBox tb = (TextBox)text_box_and_variables[i, 0];
  14. further = Double.TryParse(tb.Text, out a); // переменная "my_class.variables.main_plate_width" (и остальные переменные) не принимает нового значения
  15. if (further) { tb.BackColor = Color.White; } // здесь все ок
  16. else { tb.BackColor = Color.Pink; return false; } // здесь все ок
  17. };
  18. return true;
  19. }

Решение задачи: «Как получить переменную из массива объектов?»

textual
Листинг программы
  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             GlobalStuff.main_plate_width = new KVPair<TextBox, double>() { Key = textBox_plate_width, Value = 0 };
  4.             GlobalStuff.main_plate_length = new KVPair<TextBox, double>() { Key = textBox_plate_length, Value = 0 };
  5.             GlobalStuff.main_plate_height = new KVPair<TextBox, double>() { Key = textBox_plate_height, Value = 0 };
  6.         }
  7.  
  8.     public class KVPair<K, V>
  9.     {
  10.         public K Key { get; set; }
  11.         public V Value { get; set; }
  12.     }
  13.  
  14.     public static class GlobalStuff
  15.     {
  16.         public static KVPair<TextBox, double> main_plate_width;
  17.         public static KVPair<TextBox, double> main_plate_length;
  18.         public static KVPair<TextBox, double> main_plate_height;
  19.     }
  20.  
  21.     public static class my_class
  22.     {
  23.         public static void calculate()
  24.         {
  25.             KVPair<TextBox, double>[] stuff = new KVPair<TextBox, double>[]
  26.             {
  27.                 GlobalStuff.main_plate_width,
  28.                 GlobalStuff.main_plate_length,
  29.                 GlobalStuff.main_plate_height
  30.             };
  31.  
  32.             for (int i = 0; i < 3; i++)
  33.             {
  34.                 stuff[i].Key.BackColor = Color.Pink;
  35.  
  36.                 stuff[i].Value = 9;
  37.             };
  38.         }
  39.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 4.231 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы