473,233 Members | 1,504 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,233 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 1400
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....
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.