Thanks to all for their replies.
Jim
> -Original Message-
> From: Jim Arnold [mailto:[EMAIL PROTECTED]]
> Sent: 24 May 2002 18:00
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] Accessing struct properties
>
>
> Can someone help me get my tiny brain around
Federico Raggi wrote:
> Interestingly, if you define Foo as a class instead of a struct then Foo
> [0].Bar can be modified and has the correct "bar" value after the loop. If
> Foo is an struct it doesn't.
That's because in the direct array access code, the structs are being
directly modified. In
The problem is a restriction on the foreach construct: it doesn't allow
modifications to the elements you are iterating on, see [1].
Interestingly, if you define Foo as a class instead of a struct then Foo
[0].Bar can be modified and has the correct "bar" value after the loop. If
Foo is an struct
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 t
[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
Jim Arnold wrote:
> but *not* this:
> Foo[] foos = new Foo[1];
> foos[0] = new Foo();
> foreach(Foo foo in foos) {
> foo.Bar = "bar";
> }
> The compiler error is distinctly unhelpful: "The left-hand side of an
> assignment must be a variable, property or indexer" - which it plainly is.
>F
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";
and this:
Foo[] foos = new Foo[1];
foos[0] = new