472,121 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Cannot assign to 's' because it is a 'foreach iteration variable'

Hi,

What is wrong with the following -

short[] arrfile = new short[100];

foreach (short s in arrfile)
{
s = 10;
}

Error 1 Cannot assign to 's' because it is a 'foreach iteration
variable'

Thanks,

Barry.

Jun 2 '07 #1
4 21687
Exactly what it says.

The language does not allow you to reassign a "foreach" variable. Even
if you could, it wouldn't update the contents of hte array. What do
you want to do here? If you want to change the values in the array,
then you will have to do somehing like:

for(int i = 0 ; i < arrfile.Length; i++) {
arrfile[i] = 10;
}

Marc

Jun 2 '07 #2
Good point Marc.. Thanks

Jun 2 '07 #3
On Jun 2, 1:31 pm, Hakan Fatih YILDIRIM <hfysilis...@gmail.comwrote:
Good point Marc.. Thanks
Hi,

from MSDN:
"This error occurs when an assignment to variable occurs in a read-
only context. Read-only contexts include foreach iteration variables,
using variables, and fixed variables. To resolve this error, avoid
assignments to a statement variable in using blocks, foreach
statements, and fixed statements."

The foreach keyword just enumerates IEnumerable instances (getting an
IEnumerator instances by calling the GetEnumerator() method).
IEnumerator is read-only, therefore values can't be changed using
IEnumerator =can't be changed using the foreach context.

Hope this helps.
Moty

Jun 2 '07 #4
On Jun 2, 4:01 pm, Moty Michaely <Moty...@gmail.comwrote:
On Jun 2, 1:31 pm, Hakan Fatih YILDIRIM <hfysilis...@gmail.comwrote:
Good point Marc.. Thanks

Hi,

from MSDN:
"This error occurs when an assignment to variable occurs in a read-
only context. Read-only contexts include foreach iteration variables,
using variables, and fixed variables. To resolve this error, avoid
assignments to a statement variable in using blocks, foreach
statements, and fixed statements."

The foreach keyword just enumerates IEnumerable instances (getting an
IEnumerator instances by calling the GetEnumerator() method).
IEnumerator is read-only, therefore values can't be changed using
IEnumerator =can't be changed using the foreach context.

Hope this helps.
Moty
This is the IEnumerator problem I've faced. I guess this is a typical
case where we need to go for 'for' loop instead of 'foreach'.

Jun 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Patrick Russell | last post: by
5 posts views Thread by Brad Williams | last post: by
27 posts views Thread by Tripper | last post: by
13 posts views Thread by Michael Brown | last post: by
7 posts views Thread by Robert Bravery | last post: by
5 posts views Thread by =?Utf-8?B?RkxEYXZlTQ==?= | last post: by
9 posts views Thread by news.microsoft.com | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.