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

9
  • all data are same type? Commented Jan 25, 2017 at 16:27
  • yes, they're integers Commented Jan 25, 2017 at 16:27
  • 2
    so, you can use Dictionary. But why you want to copy it form array? Commented Jan 25, 2017 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. Commented Jan 25, 2017 at 16:31
  • 1
    But if you have array why you need variables? Commented Jan 25, 2017 at 16:37

1 Answer 1

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];
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.