473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Issue foreach loop

Hi ,

I am confused that's is not even correctly mentioned in MSDN , if my
code snippet works , have a look at this :

1. string s = {"1", "2","3"} ;

foreach(string ss in s)
{
ss = "4" ; // Compilation error as foreach loop is Read Only as
// it just associates an Enumerator .
}

Now above example is perfect and that's what MSDN says .

Now try this for any simple DataSet (ds) retrieved from a db .

foreach(DataRow dr in ds.Tables[0].Rows)
{
dr[0] = "Something" ; // Assume string column
dr.AcceptChanges() ; // it works fine at every level , so //
compilation error , no runtime issue
// whcih logically shouldn't be an issue
}

any pointers to help me out on this , can it be a MS issue , i am not
even sure about the reason for differentiating behaviour .

thanks ,

Mrinal
Feb 1 '06 #1
4 1403
Mrinal Kamboj wrote:
Hi ,

I am confused that's is not even correctly mentioned in MSDN , if my
code snippet works , have a look at this :

1. string s = {"1", "2","3"} ;

foreach(string ss in s)
{
ss = "4" ; // Compilation error as foreach loop is Read Only
as // it just associates an Enumerator .
}

Now above example is perfect and that's what MSDN says .

Now try this for any simple DataSet (ds) retrieved from a db .

foreach(DataRow dr in ds.Tables[0].Rows)
{
dr[0] = "Something" ; // Assume string column
dr.AcceptChanges() ; // it works fine at every level ,
so // compilation error , no runtime issue
// whcih logically shouldn't be an issue
}

any pointers to help me out on this , can it be a MS issue , i am not
even sure about the reason for differentiating behaviour .
Perhaps in the first example, ss is a Value,
than in second example, dr is an Object.

cyrille
thanks ,

Mrinal

Feb 1 '06 #2
Mrinal Kamboj wrote:
I am confused that's is not even correctly mentioned in MSDN , if my
code snippet works , have a look at this :

1. string s = {"1", "2","3"} ;

foreach(string ss in s)
{
ss = "4" ; // Compilation error as foreach loop is Read Only as
// it just associates an Enumerator .
}

Now above example is perfect and that's what MSDN says .

Now try this for any simple DataSet (ds) retrieved from a db .

foreach(DataRow dr in ds.Tables[0].Rows)
{
dr[0] = "Something" ; // Assume string column
dr.AcceptChanges() ; // it works fine at every level , so //
compilation error , no runtime issue
// whcih logically shouldn't be an issue
}

any pointers to help me out on this , can it be a MS issue , i am not
even sure about the reason for differentiating behaviour .


There's no different behaviour there. In both cases, the variable
itself is readonly. However, that doesn't stop you from accessing
properties etc of the object that the variable refers to.

If you tried:

dr = ds.Tables[0].NewRow();

then you would be changing the value of the variable, and that would be
disallowed.

It's very important to distinguish between a variable, its value, and
an object which a variable's value may be a reference to.

Jon

Feb 1 '06 #3
There is no difference in behaviour (and in both cases "ss" and "dr" are
reference-type objects); you are doing very different things and getting the
different behaviour:

in the first example you are *reassigning the variable* "ss" - i.e. ss =
in the second example you are manipulating properties etc of dr *without*
reassigning it

If you typed "dr = null;" inside the foreach, that would fail for the same
reason.

Note also that strings are immutable, so pretty-much any operation is going
to be assignment; ss+="!"; would fail for the same reason, as this doesn't
read "append exclamation to ss", but rather "build a new string by
concatenating ss and exclamation, and assign this new string to ss".

Does that make sense?

Marc
Feb 1 '06 #4
Thanks Marc & Jon,

Your explanations are clear and usefull !

cyrille
Feb 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
3
by: AK | last post by:
Hi Everyone, For Last a few weeks I have been involved with performance tuning of an application. I have observed that if I do a loop over a collection (ArrayList/HashTable) which has 50000...
0
by: d pak | last post by:
Here is a snippit which replicates my issue. I have a datagrid which contains an input textbox on each row, binded on the serverside. However it seems that when I perform a postback to refresh teh...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
0
by: vigneshrao | last post by:
Hi, I have been working on a script that loops through multiple records and sends data (one record per call) to a WS. I am supposed to make a new call for each record before sending the data....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.