21

When I try to do this...

Item[,] array = new Item[w, h];  // Two dimensional array of class Item, 
                                 //   w, h are unknown at compile time.
foreach(var item in array)
{
    item = new Item();
}

...I get Cannot assign to 'item' because it is a 'foreach iteration variable'.

Still, I'd like to do that.

The idea is to assign default Item class values to existing item.

3
  • Why dont you just use another variable? Commented Aug 4, 2012 at 17:56
  • var myItem; foreach (var item in twoDimArray) myItem = new Item(); Commented Aug 4, 2012 at 17:57
  • 1
    It's not clear what you're really trying to achieve. Are you hoping to change the contents of the array? Commented Aug 4, 2012 at 17:59

4 Answers 4

29

Okay, now that we know your aim instead of how you were trying to achieve it, it's much easier to answer your question: you shouldn't be using a foreach loop. foreach is about reading items from a collection - not changing the contents of a collection. It's a good job that the C# compiler makes the iteration variable read-only, otherwise it would have let you change the value of the variable without that actually changing the collection. (There'd have to be more significant changes to allow changes to be reflected...)

I suspect you just want:

for (int i = 0; i < array.GetLength(0); i++)
{
    for (int j = 0; j < array.GetLength(1); j++)
    {
        array[i, j] = new Item();
    }
}

That's assuming it's a rectangular array (an Item[,]). If it's an Item[][] then it's an array of arrays, and you'd handle that slightly differently - quite possibly with a foreach for the outer iteration:

foreach (var subarray in array)
{
    for (int i = 0; i < subarray.Length; i++)
    {
        subarray[i] = new Item();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Lets see if this old answer will get some attention. Your answer is obvious for a 2-dimensional array but is there a clever answer for an n-dimensional array?
@LamdaComplex: Well you could build an array of indexes, and effectively increment that - or use recursion. I suggest you ask a new question, with a short but complete example of what you're trying to achieve.
6

Not knowing the size isn't a problem:

for (int i = 0; i < twoDimArray.GetLength(0); i++)
{
    for (int j = 0; j < twoDimArray.GetLength(1); j++)
    {
        twoDimArray[i, j] = ...
    }
}

Comments

2

It looks like you're trying to initialize the array. You can't do that this way. Instead, you need to loop through the array by index.

To initialize the elements of a given two-dimensional array, try this:

for (int d = 0; d < array.GetLength(0); d++)
{
    for (int i = 0; i < array.GetLength(1); i++)
    {
        array[d, i] = new Item();
    }
}

Comments

1

You can use normal for loop for that.

1 Comment

it's a two-dimensional array, so you can find out the length, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.