[RESOLVED] Form a variable name using concatenation
The variable in question is an array. Let's say I want to loop through a series of arrays (array1; array2; array3; etc.). I want to form the loop like this:
Code:
for (int i = 0; i < 4; i++)
{
currentXUBound = array + i.GetUpperBound(0);
currentYUBound = array + i.GetUpperBound(1);
//do other processes here...
}
This doesn't work because of the way I've concatenated the variable name. What is the correct way to express this in C#?
Re: Form a variable name using concatenation
You don't. Variable names are something you write explicitly in your code. Arrays are objects, just like any other. If you have multiple arrays that you want to select from then you put those arrays into an array or collection and then select one by index or key.
Re: Form a variable name using concatenation
The way to loop over a series of object would be to add them to a collection that supports IEnumerable, such as List<T>:
csharp Code:
int[,] array1 = new int[3,4];
int[,] array2 = new int[9,8];
int[,] array3 = new int[2,2];
int[,] array4 = new int[6,9];
List<int[,]> arrays = new List<int[,]>() {array1, array2, array3, array4};
foreach (int[,] currentArray in arrays)
{
int currentXUBound = currentArray.GetUpperBound(0);
int currentYUBound = currentArray.GetUpperBound(1);
// do other processes here...
}
Re: Form a variable name using concatenation
That's exactly what I needed. Thanks.