Imagine we have 5 string variables and we want to assign ‘Foo’ to them during runtime.
Instead of this
string a1 = 'Foo'; string a2 = 'Foo'; string a3 = 'Foo'; string a4 = 'Foo'; string a5 = 'Foo';
Can’t we use something like this:
for(int i = 0;i < 5;i++) { a+i = 'Foo'; }
Is it impossible to access them in a similiar way?
As others have said, an array (or another collection) is a much better way of doing this.
Think of an array as just being a collection of variables which you always access by index.
I can’t think of any reason why you’d want to have a set of variables as shown in your question. If you absolutely have to do that, and they’re instance/static variables, you could use reflection to access them if you really wanted to – but using an array or other collection would be much, much better.