Dynamically changing Variable name

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

Hello,
I have ArrayLists that are Channel1, Channel 2......Channel16
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
 
I have ArrayLists that are Channel1, Channel 2......Channel16
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
 
AMP said:
Hello,
I have ArrayLists that are Channel1, Channel 2......Channel16
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.
 
Hello,
I have ArrayLists that are Channel1, Channel 2......Channel16
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
 
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.

To be honest, it sounds like you need Board type - and then a list of
boards. Any time you end up with something 2-dimensional (or appearing
to be) it's worth considering whether an extra bit of encapsulation
would help.
 
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.

To be honest, it sounds like you need Board type - and then a list of
boards. Any time you end up with something 2-dimensional (or appearing
to be) it's worth considering whether an extra bit of encapsulation
would help.

So,
I am taking your advise and now that I cleaned the data I want to put
it back into an Array from Array list.
But the Array is going to be 2 dimentional:
int[][] BoardChannel = new int[16][];
I want to pu each of the 16 Channel ArrayLists into it.
HELP.
Thanks
Mike
 
I am taking your advise and now that I cleaned the data I want to put
it back into an Array from Array list.
But the Array is going to be 2 dimentional:
int[][] BoardChannel = new int[16][];
I want to pu each of the 16 Channel ArrayLists into it.
HELP.

If you are taking my advice, you won't have 16 channel ArrayLists.
You'll have a single ArrayList populated with Boards.

I'm afraid you'll need to clarify what exactly you want to achieve -
it's not very clear at the moment.
 
I am taking your advise and now that I cleaned the data I want to put
it back into an Array from Array list.
But the Array is going to be 2 dimentional:
int[][] BoardChannel = new int[16][];
I want to pu each of the 16 Channel ArrayLists into it.
HELP.

If you are taking my advice, you won't have 16 channel ArrayLists.
You'll have a single ArrayList populated with Boards.

I'm afraid you'll need to clarify what exactly you want to achieve -
it's not very clear at the moment.

What I have is data that goes through a number of functions to clean
and rearrange the data. This is for a physics experiment.
The Arraylist is a great way to keep the array size to a minimal
automatically as I work on it.
But after I have all 16 channels cleaned I want to put them back into
an Array to graph them.
Does this help?
Thanks
Mike
 
What I have is data that goes through a number of functions to clean
and rearrange the data. This is for a physics experiment.
The Arraylist is a great way to keep the array size to a minimal
automatically as I work on it.
But after I have all 16 channels cleaned I want to put them back into
an Array to graph them.
Does this help?

Not really, I'm afraid. Why do you need them in an Array? Does your
graphing library (or whatever) really not accept other collections?

However, if you have to, I'd add a method to your Board class -
something like:

public int[] ToArray()
{
...
}

Then potentially call ToArray on each board, building up an ArrayList
of int[]s, then call ToArray on *that* ArrayList.

(Are you stuck with .NET 1.1, by the way? It would be neater with
List<T> in in .NET 2.0. With .NET 3.5 you could quite possibly do it
all *really* nicely with LINQ, and then automatically parallelise
it...)
 
AMP said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top