Dynamically changing Variable name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AMP

    Dynamically changing Variable name

    Hello,
    I have ArrayLists that are Channel1, Channel 2......Channel1 6
    I want to access them with a (for int s=1;s<17;s++) loop, but I dont
    know how to do this:
    "Channel+s"
    Any help,
    Thanks
    Mike
  • Jon Skeet [C# MVP]

    #2
    Re: Dynamically changing Variable name

    On Mar 17, 3:01 pm, AMP <ampel...@gmail .comwrote:
    I have ArrayLists that are Channel1, Channel 2......Channel1 6
    I want to access them with a (for int s=1;s<17;s++) loop, but I dont
    know how to do this:
    "Channel+s"
    You shold almost certainly be using an array, a list or a dictionary
    instead. Why do you feel you *want* to have 16 different variables
    rather than one?

    Jon

    Comment

    • Michael A. Covington

      #3
      Re: Dynamically changing Variable name

      "AMP" <ampeloso@gmail .comwrote in message
      news:f50a93eb-83ad-477d-8417-3a5872609ff4@v3 g2000hsc.google groups.com...
      Hello,
      I have ArrayLists that are Channel1, Channel 2......Channel1 6
      I want to access them with a (for int s=1;s<17;s++) loop, but I dont
      know how to do this:
      "Channel+s"
      Any help,
      Thanks
      Mike
      Variable names are not known at run time. That is a basic principle of
      compiled programming languages. (In a language with reflection, such as C#,
      it may be possible to retrieve a variable name, but not to change it.)

      What you want is an array of ArrayLists: Channel[0][...etc...],
      Channel[1][...etc...], and so on.


      Comment

      • AMP

        #4
        Re: Dynamically changing Variable name

        On Mar 17, 12:23 pm, "Michael A. Covington" <m...@uga.eduwr ote:
        "AMP" <ampel...@gmail .comwrote in message
        >
        news:f50a93eb-83ad-477d-8417-3a5872609ff4@v3 g2000hsc.google groups.com...
        >
        Hello,
        I have ArrayLists that are Channel1, Channel 2......Channel1 6
        I want to access them with a (for int s=1;s<17;s++) loop, but I dont
        know how to do this:
        "Channel+s"
        Any help,
        Thanks
        Mike
        >
        Variable names are not known at run time.  That is a basic principle of
        compiled programming languages.  (In a language with reflection, such asC#,
        it may be possible to retrieve a variable name, but not to change it.)
        >
        What you want is an array of ArrayLists:  Channel[0][...etc...],
        Channel[1][...etc...], and so on.
        Actually the data started out as Arrays and I am finding ArrayLists
        allow me to clean the data easier.
        I have 12 boards with 16 channels on each board.The amount of data
        changes between channels and boards.
        I should still be able to use some kind of "for statenent to do my
        cleansing on a "Channel+i" basis.
        Right?
        Thanks
        Mike

        Comment

        • Steve Gerrard

          #5
          Re: Dynamically changing Variable name

          AMP wrote:
          On Mar 17, 12:23 pm, "Michael A. Covington" <m...@uga.eduwr ote:
          >"AMP" <ampel...@gmail .comwrote in message
          >>
          >What you want is an array of ArrayLists: Channel[0][...etc...],
          >Channel[1][...etc...], and so on.
          >
          Actually the data started out as Arrays and I am finding ArrayLists
          allow me to clean the data easier.
          I think you missed that AMP wrote "an array of ArrayLists."
          If you wanted, though, you could also have "an ArrayList of ArrayLists."

          1. Create a new ArrayList called Temp
          2. Add Channel1 to it, add Channel2 to it, ....
          3. Do a loop on Temp, processing each channel in turn
          4. Toss Temp, unless you want to keep it


          Comment

          Working...