[inline]

-- Brent Rector, .NET Wise Owl
Demeanor for .NET - an obfuscation utility
http://www.wiseowl.com/Products/Products.aspx



-----Original Message-----
From: Jim Arnold [mailto:[EMAIL PROTECTED]] 
Sent: Friday, May 24, 2002 10:00 AM
To: [EMAIL PROTECTED]
Subject: [DOTNET] Accessing struct properties


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?

cheers,

Jim

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

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