1

First off I'm sure that I am using the wrong terminology here but I will fix it if someone comments on it. Please be gentle.

So I have multiple charts on a page and I am performing virtually identical actions on each. For demonstrative purposes lets call my charts something like: chart1, chart2, ..., chartn where n is somewhere in the vicinity of 20. What I would like to do is drop this in a for loop and perform all the work in one smaller chunk of code, especially if I have to tweak it later.

So my question is whether or not I can vary the n part representing the object (terminology?) so I can get this done more efficiently.

i.e.:

for(int i = 0; i < 20; i++)
{
    String chartName = "chart" + i;
    chartName.Series.Clear();
}

I have a feeling you can't do this with a string so I was looking into doing a foreach but I don't know how to do this with charts.

Thanks a lot!

tmwoods
  • 2,353
  • 7
  • 28
  • 55
  • I'm really not sure what the question is here. Can you try and provide some more information? – Jonathon Reinhart Mar 20 '13 at 03:28
  • 2
    Your really should use array/list/collection of objects... But take a look at my previous answer to similar question if you really need unique variables - http://stackoverflow.com/questions/15424570/c-sharp-how-to-treat-ints-like-they-are-array/15424657#15424657 – Alexei Levenkov Mar 20 '13 at 03:29
  • I'm guessing you have variables named `chart1`, `chart2`, etc.. and you want to loop through them? And your idea was: if I could build the name `"chart1"` somehow as a string, and resolve that to the actual `chart1` variable, then I can work with it. Right? – Daniel A.A. Pelsmaeker Mar 20 '13 at 03:31
  • Oops, Virtlink has already given you the answer. – Zeddy Mar 20 '13 at 03:32

2 Answers2

4

You should put the charts in a list. For example, this makes a list of Chart objects (or whatever your chart type is):

List<Chart> charts = new List<Chart>();

Then you can add charts:

charts.Add(new Chart());

And use them:

for (int i = 0; i < charts.Count; i++)
{
    charts[i].Series.Clear();
}

Of course, you can make the charts variable a field in your class.


You can directly initialize a list (or array, or dictionary1) like this:

List<Chart> charts = new List<Charts>()
{
    new Chart(),
    new Chart(),
    existingChart1,
    existingChart2
};

Or, if you create a new array of objects using that syntax...

Chart[] arrayOfCharts = new []
{
    new Chart(),
    new Chart(),
    existingChart1,
    existingChart2
};

...then you can add multiple objects at once using AddRange:

charts.AddRange(arrayOfCharts);

1) You can use this so-called collection initializer syntax on any object that has a public Add method.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • While I`m at it, I am curious about something (I don`t really need it here). Can you add multiple objects to a list at once? i.e. `charts.Add(chart1, chart2, chart3)` or something like this? I know what I just wrote doesn't work, but is there another way? Sort of like declaring several variables at once. Thanks! – tmwoods Mar 20 '13 at 04:32
  • 1
    `charts.AddRange(new [] {chart1, chart2, chart3 })` will allow you to add a number of chart objects to the charts list. – Matt Mar 20 '13 at 05:41
0

Can you access your chart from a list/array/collection of charts like this?

for (int i = 0; i <= 19; i++) {
  String chartName = "chart" + i;
  Charts(chartName).Series.Clear();
}

or maybe

for (int i = 0; i <= 19; i++) {
  String chartName = "chart" + i;
  Charts(i).Series.Clear();
}
Zeddy
  • 2,079
  • 1
  • 15
  • 23