471,317 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 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 1336
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Randell D. | last post: by
10 posts views Thread by Eric | last post: by
104 posts views Thread by cody | last post: by
reply views Thread by rosydwin | 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.