Adding a variable to a variable name
-
Is there a way to add a variable to a variable name reference? I'm trying to cut out a rats nest of if statements. When I searched, I found this but it didn't look like what I want: http://www.windows-tech.info/1/ae8c74ec5a29a601.php[^] This is a code snippet to get an idea of what I'm talking about:
//count Good or NG program result per shift foreach (Shift shift in shiftArr) { //iterate through array of Shift Structs if (shift.shift == currentShift) { if (programResult == 0) //0 == No Good (NG) { shift.NG\_Count++; tb\_Shift\_$(currentShift)\_Count\_NG = Convert.ToString(shift.NG\_Count); //this doesn't work but maybe there's a way } else { shift.good\_Count++; tb\_Shift\_$(currentShift)\_Count\_G = Convert.ToString(shift.G\_Count); //issue too } } }
-
Is there a way to add a variable to a variable name reference? I'm trying to cut out a rats nest of if statements. When I searched, I found this but it didn't look like what I want: http://www.windows-tech.info/1/ae8c74ec5a29a601.php[^] This is a code snippet to get an idea of what I'm talking about:
//count Good or NG program result per shift foreach (Shift shift in shiftArr) { //iterate through array of Shift Structs if (shift.shift == currentShift) { if (programResult == 0) //0 == No Good (NG) { shift.NG\_Count++; tb\_Shift\_$(currentShift)\_Count\_NG = Convert.ToString(shift.NG\_Count); //this doesn't work but maybe there's a way } else { shift.good\_Count++; tb\_Shift\_$(currentShift)\_Count\_G = Convert.ToString(shift.G\_Count); //issue too } } }
Well, that looks almost like you are trying to create a list of Count_NG and Count_G items. Perhaps that might help.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Well, that looks almost like you are trying to create a list of Count_NG and Count_G items. Perhaps that might help.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
I already have them in a struct according to shift. Is there a way to use the currentShift variable to add the data to the text box? Maybe I can add the tb variable name to the struct? But then that would be changing in name every shift and not fit in with the code to process the G/NG per shift. I have 3 different tb's, one for each shift, for each G/NG count. The struct looks like this:
\[StructLayout(LayoutKind.Sequential)\] public class Shift { public char shift; //A, B, C public int good\_Count = 0; public int NG\_Count = 0; }
-
I already have them in a struct according to shift. Is there a way to use the currentShift variable to add the data to the text box? Maybe I can add the tb variable name to the struct? But then that would be changing in name every shift and not fit in with the code to process the G/NG per shift. I have 3 different tb's, one for each shift, for each G/NG count. The struct looks like this:
\[StructLayout(LayoutKind.Sequential)\] public class Shift { public char shift; //A, B, C public int good\_Count = 0; public int NG\_Count = 0; }
You cannot modify a variable name at runtime to point to a different value. Perhaps a List or a Dictionary would be a better choice.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Is there a way to add a variable to a variable name reference? I'm trying to cut out a rats nest of if statements. When I searched, I found this but it didn't look like what I want: http://www.windows-tech.info/1/ae8c74ec5a29a601.php[^] This is a code snippet to get an idea of what I'm talking about:
//count Good or NG program result per shift foreach (Shift shift in shiftArr) { //iterate through array of Shift Structs if (shift.shift == currentShift) { if (programResult == 0) //0 == No Good (NG) { shift.NG\_Count++; tb\_Shift\_$(currentShift)\_Count\_NG = Convert.ToString(shift.NG\_Count); //this doesn't work but maybe there's a way } else { shift.good\_Count++; tb\_Shift\_$(currentShift)\_Count\_G = Convert.ToString(shift.G\_Count); //issue too } } }
-
So you have variables called tb_Shift_1_Count_NG, tb_Shift_2_Count_NG etc? Surely you could call them tb_Shift[1].Count_NG ... and have a list of Shift objects instead of a whole bunch of variables.
-
I'd love to implement it that way, except tb_Shift_A_Coung_NG and tb_Shift_B_Count_NG are the names for the textbox. I don't think I can use an array for those, can I?
You can have an array (or list) of textboxes. That's not a problem.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier