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.AcceptChange s() ; // 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 4 1415
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.AcceptChange s() ; // 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
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.AcceptChange s() ; // 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
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
Thanks Marc & Jon,
Your explanations are clear and usefull !
cyrille This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 code everytime I found more than one
method to perform a single task. I timed each method to find which would
complete faster. I thought I'd share my most recent results which (I
believe) should help those write their programs to be more...
|
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsicollectionclasssyncroottopic.asp)
And I'm interested in locking an ArrayList during the entire enumeration, as
shown in the example code. My problem is that I'm STILL...
|
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
foeach(Color c in Red..Blue) { } // using enums
It should work with all integral datatypes. Maybe we can step a bit further:
foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
|
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 "foreach" loop instead of a "for" loop. This made absolutely no sense
to me, but the author had posted his code and timing results. The "foreach"
(a VB and other languages construct) was 0.01 seconds to access 1000 lines in
rich text box,...
|
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 findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}
Thanks for any help.
| |
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 objects in one shot then it takes about
15 minutes to do the computation. But if I break this to loop of only 5000
objects and do all computation in 10 loops of 5000 each then it takes about 1
minutes 30 seconds.
I am trying to find a reason for...
|
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 numbers, they are not changing due
to the foreach loop I have in this code. When I comment out the foreach loop,
the numbers refresh fine (but the text boxs ovbiously no longer get created).
I have placed this code in the OnItemDataBind method,...
|
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 code is here:
foreach ( string propertyName in ht.Keys )
{
this.setProperty( propertyName, ht );
|
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.
The problem I have is the first record gets processed fine where as the second record always; reason being the EAI expects it to be a seperate call
Though I am creating/reseting a new service everytime within the foreach loop the data seems to be...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |