Re: foreach issue

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 08/12/04


Date: Thu, 12 Aug 2004 11:05:35 -0400

Jay,

It is read-only just to prevent you for making mistakes.

*e* (in your example) is nothing more than a local variable. You may change
its value, but it won't affect the reference kept in the collection. Having
it read only let the compiler emit an error if this happens.

e.SomeProp = ...; at the other hand is correct code. However whether it is
going to chanage the object in the collection depends on what is the type of
the collection (array or non-typed collection) and what is the type of the
item (reference or value type).

-- 
HTH
Stoitcho Goutsev (100) [C# MVP]
"Jay" <j@h.com> wrote in message news:a8LSc.10$xs.0@okepread02...
> When doing a foreach statement like the following why is the variable
> read-only?
>
> foreach (e in theArray)
>
> What if I have an array of objects and I need to create a new instance of
> the object.  If the e is read-only then I have to use a for statement to
> accomplish this.
>
>


Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)
  • Re: Perlish map() function
    ... the reference material, the Array constructor was introduced in JavaScript ... evaluation would result in a ReferenceError. ... identifier shows that it is true. ...
    (comp.lang.javascript)