Как получить переменную из массива объектов? - C#
Формулировка задачи:
Создал массив объектов. В массив запихнул [TextBox, double].
Далее перебираю в цикле элементы массива.
У меня не получается в глобальные переменные (my_class.variables.main_plate_width... ) передать значение. Как это сделать?
Листинг программы
- public static bool calculate()
- {
- bool further;
- object[,] text_box_and_variables = new object[,]
- {
- { my_class.text_box.main_plate_width, my_class.variables.main_plate_width },
- { my_class.text_box.main_plate_length, my_class.variables.main_plate_length },
- { my_class.text_box.main_plate_height, my_class.variables.main_plate_height }
- };
- for (int i = 0; i < 2; i++)
- {
- double a = (double)text_box_and_variables[i, 1]; // здесь наверное проблема
- TextBox tb = (TextBox)text_box_and_variables[i, 0];
- further = Double.TryParse(tb.Text, out a); // переменная "my_class.variables.main_plate_width" (и остальные переменные) не принимает нового значения
- if (further) { tb.BackColor = Color.White; } // здесь все ок
- else { tb.BackColor = Color.Pink; return false; } // здесь все ок
- };
- return true;
- }
Решение задачи: «Как получить переменную из массива объектов?»
textual
Листинг программы
- private void Form1_Load(object sender, EventArgs e)
- {
- GlobalStuff.main_plate_width = new KVPair<TextBox, double>() { Key = textBox_plate_width, Value = 0 };
- GlobalStuff.main_plate_length = new KVPair<TextBox, double>() { Key = textBox_plate_length, Value = 0 };
- GlobalStuff.main_plate_height = new KVPair<TextBox, double>() { Key = textBox_plate_height, Value = 0 };
- }
- public class KVPair<K, V>
- {
- public K Key { get; set; }
- public V Value { get; set; }
- }
- public static class GlobalStuff
- {
- public static KVPair<TextBox, double> main_plate_width;
- public static KVPair<TextBox, double> main_plate_length;
- public static KVPair<TextBox, double> main_plate_height;
- }
- public static class my_class
- {
- public static void calculate()
- {
- KVPair<TextBox, double>[] stuff = new KVPair<TextBox, double>[]
- {
- GlobalStuff.main_plate_width,
- GlobalStuff.main_plate_length,
- GlobalStuff.main_plate_height
- };
- for (int i = 0; i < 3; i++)
- {
- stuff[i].Key.BackColor = Color.Pink;
- stuff[i].Value = 9;
- };
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д