473,473 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 22173
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Patrick Russell | last post by:
I am trying to figure out why the code listed below works in php v4.3.0 but not in v4.3.10: $string = "Param1=Val1;Param2=Val2;Param3=Val3"; $exploded_string = explode(";",$string);...
5
by: Brad Williams | last post by:
I'm trying to get clearer on limitations of assignment/modifications within a foreach. Why does the following gives a compilation error if MyType is a struct, but it does not if MyType is a class?...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
13
by: Michael Brown | last post by:
Hi there. Can anybody comment on the efficiency of the following technique: foreach (SomeItem Item in SomeNativeDotNet.Collection) { // Whatever } My concern is that the "Collection"...
7
by: Robert Bravery | last post by:
HI all, If I have a foreach loop, is there a way to tell what number of itteration I am on during each loop Thanks Robert
5
by: =?Utf-8?B?RkxEYXZlTQ==?= | last post by:
I'm developing an application that gets data from 100 sources (via telnet connections, but you can think stock quotes from a webservice if you like). I was planning on using the thread pool (25...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
2
by: John Kelsey | last post by:
I am an old, longtime C programmer surprised and confused by an error message I'm getting from my VS2005 compiler... "Cannot pass 'Item' as a ref or out argument because it is a 'foreach...
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.