Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Adding a variable to a variable name

Adding a variable to a variable name

Scheduled Pinned Locked Moved C#
phpdata-structureshelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MichCl
    wrote on last edited by
    #1

    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
    
                    }
                }
            }
    
    P B 2 Replies Last reply
    0
    • M MichCl

      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
      
                      }
                  }
              }
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        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

        M Offline
        M Offline
        MichCl
        wrote on last edited by
        #3

        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;
            }
        
        D 1 Reply Last reply
        0
        • M MichCl

          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;
              }
          
          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M MichCl

            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
            
                            }
                        }
                    }
            
            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            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.

            M 1 Reply Last reply
            0
            • B BobJanova

              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.

              M Offline
              M Offline
              MichCl
              wrote on last edited by
              #6

              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?

              P 1 Reply Last reply
              0
              • M MichCl

                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?

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups