-3

I can't find information about this. I need to make a lot of items, and it looks like this:

Bar01.Value = data[0];
Bar02.Value = data[1];
Bar03.Value = data[2];
Bar04.Value = data[3];
Bar05.Value = data[4];
Bar06.Value = data[5];
...
Bar99999.Value = data[99998]
etc.

Is there a way to make a string for it like

for(int i=0;i<max;i++)
string s = "Bar"+i;
//do stuff

So that it would be shorter ?

Dawid
  • 585
  • 5
  • 26
  • all data are same type? – BWA Jan 25 '17 at 16:27
  • yes, they're integers – Dawid Jan 25 '17 at 16:27
  • 2
    so, you can use Dictionary. But why you want to copy it form array? – BWA Jan 25 '17 at 16:28
  • There is usually never a goood reason to instantiate a ridiculously large collection in memory. Better to create a Factory type that can return a specific item on request (based on some TBD parameters). As the question stands now what you are actually trying to accomplish is not very clear. – Igor Jan 25 '17 at 16:31
  • If you think you want to have 99,999 variables... don't. If you tell us how they are used, we might be able to give you a better solution. – crashmstr Jan 25 '17 at 16:32
  • Oh, I just made this up quickly to show the problem I had. I'm actually using an array of 32 integers, and creating 32 lines of ctrl+c/ctrl+v code seemed silly to me – Dawid Jan 25 '17 at 16:33
  • you should have better solution. Don't waste variable like this. – Duong Dang Jan 25 '17 at 16:34
  • 1
    But if you have array why you need variables? – BWA Jan 25 '17 at 16:37
  • What represents "Barxx"? are those controls? classes? something else? – Gusman Jan 25 '17 at 16:37

1 Answers1

3

You can use Dictionary in this way:

Dictionary<string, int> dict = new Dictionary<string, int>();
for(int i=0;i<max;i++)
{
    dict["Bar" + i] = data[i];
}
BWA
  • 5,672
  • 7
  • 34
  • 45