Jim,

I was able to get the last example to compile with the following
modification:

Foo[] foos = new Foo[1];
foos[0] = new Foo();

foreach(Object obj in foos) {
    Foo foo     = (Foo) obj;
    foo.Bar = "bar";
}
//but foos[0].Bar is still "" here...



Note that difference is that the type of the iteration variable is
Object. In order to get to the Bar property you have to cast it to the
Foo type.

Even with this "solution", the value of Bar is not being updated (as
Brad Wilson indicated).
If you check the value of foos[0].Bar after the foreach loop, you will
find that it is still a zero length string, and NOT "bar".  This is due
as you suspected to some "boxing" that is going on. 

The gist is that you can't modify a value type while iterating on an
array or collection using foreach.

HTH

Larry


**********************
Snippets from Original Message ...
**********************

Can someone help me get my tiny brain around this?  Given this struct:

struct Foo {

    string _bar;

    public string Bar {
        get{return _bar;}
        set{_bar = value;}
    }
}

Why can I do this:

Foo foo = new Foo();
foo.Bar = "bar";

[Brent]
This changes the Bar property in the Foo valuetype, as expected.

and this:

Foo[] foos = new Foo[1];
foos[0] = new Foo();

for(int i=0;i<foos.Length;i++) {
    foos[i].Bar = "bar";
}

[Brent]
This changes the Bar property in the Foo valuetype in the array element,
as expected.


but *not* this:

Foo[] foos = new Foo[1];
foos[0] = new Foo();

foreach(Foo foo in foos) {
    foo.Bar = "bar";
}

[Brent]
Variable 'foo' contains a *copy* of the array element. Foo is a
valuetype. You would be changing the Bar property in the copy and never
affecting the element in the array.


The compiler error is distinctly unhelpful: "The left-hand side of an
assignment must be a variable, property or indexer" - which it plainly
is. I understand why I can't modify the value of an intermediate
expression (CS1612), but what's the deal here?  Am I getting my
pass-by-value and pass-by-ref's muddled?

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to