I am using windows forms to represent some parameters (i.e. temperature) incoming from an external device. I would like to represent this data in a windows forms labels and would like to do it this way: 1) create a label named according to data register; 2) While reading data, create a string that contains label object reference as string; 3) Make an object reference out of the string and call the text() method to update the data stored in the label.
Some example code on how I tried to do this:
// Here I create a label that is referenced by L0x7241
private System.Windows.Forms.Label L0x7241;
private void UpdateLabels(byte[] data)
{
// Convert bytes to register name
var reg = new byte[2];
reg[0] = data[1];
reg[1] = data[0];
// Here I construct a string, that contains the same text: L0x7241
string associatedWidget = "L0x" + BitConverter.ToString(reg).Replace("-", "");
// Here I would like to change text of the label
// that has the reference that i have just constructed
associatedWidget.Text("1111");
}
I understand that a string containing the same text as object reference is not the reference itself, thus I am looking for a way to convert a string to a reference if that is possible at all.
Now, I understand that I could do the same by associating each label with a variable and update them on variable change. Yet I think that the above mentioned way would make the program significantly smaller...