Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Dynamically changing Variable name

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AMP - 17 Mar 2008 16:01 GMT
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
Jon Skeet [C# MVP] - 17 Mar 2008 16:13 GMT
> 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
Michael A. Covington - 17 Mar 2008 17:23 GMT
> Hello,
> I have ArrayLists that are Channel1, Channel 2......Channel16
[quoted text clipped - 4 lines]
> 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.
AMP - 17 Mar 2008 17:54 GMT
> > Hello,
> > I have ArrayLists that are Channel1, Channel 2......Channel16
[quoted text clipped - 11 lines]
> 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
Jon Skeet [C# MVP] - 17 Mar 2008 18:02 GMT
<snip>

> > What you want is an array of ArrayLists:  Channel[0][...etc...],
> > Channel[1][...etc...], and so on.
[quoted text clipped - 5 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AMP - 17 Mar 2008 20:12 GMT
> <snip>
>
[quoted text clipped - 16 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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
Jon Skeet [C# MVP] - 17 Mar 2008 20:29 GMT
<snip>

> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AMP - 17 Mar 2008 20:43 GMT
> <snip>
>
[quoted text clipped - 14 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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
Jon Skeet [C# MVP] - 17 Mar 2008 21:17 GMT
<snip>

> What I have is data that goes through a number of functions to clean
> and rearrange the data. This is for a physics experiment.
[quoted text clipped - 3 lines]
> 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...)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Steve Gerrard - 18 Mar 2008 03:44 GMT
>> "AMP" <ampel...@gmail.com> wrote in message
>>
[quoted text clipped - 3 lines]
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.