- Home /
Variable name using another variable
I'm trying to set up arrays for all the containers in my game but having issues with using a variable name to assign another variable.
This is my code. Each container is an array with 10 slots. But I want there to be 3 containers. So if I want to see whats in a certain container I can check container_3[5] etc...
// set up containers
for (int cn = 0; cn < 3; cn++) {
for (int a = 0; a < 10; a++) {
{"container_" + cn}[a] = "blah"; <-----------------
}
}
Arrowed the line where I'm having issues, how can I do this?
Comment
If you just want to identify you arrays with an index in the name, you could just use a multi-dimensional array
Best Answer
Answer by NoseKills · Jul 10, 2015 at 07:47 AM
Make an array of your arrays, a.k.a. multidimensional array.
string[][] containers = new string[3][];
for (int cn = 0; cn < 3; cn++) {
containers[cn] = new string[10];
for (int a = 0; a < 10; a++) {
containers[cn][a] = "blah"; <-----------------
}
}
Answer by taxvi · Jul 10, 2015 at 07:45 AM
use HashTable or Dictionary, if you don't know what these are learn them, they are important