J
J.Marsch
Ok, I think that what I'm seeing is due to some implicit boxing/unboxing
going on, but I'd like to understand this a little better.
I have a collection of structs. If I try to iterate the collection of
structs with a foreach() loop, and in the loop I try to change the value of
one of the fields in the struct, I get a compile time error "The left-hand
side of an assignment must be a variable, property, or indexer". (a very
contrived code example at the end of the email)
I _think_ that I understand what the issue is, but I'd like some validation.
I think that the problem is that the collection that I am using uses type
object as its inherent storage type, so when you add a struct to the
collection, it is being boxed implicitly. Then, when you retrieve the
struct from the collection in a foreach, that struct is being implicitly
unboxed. If you try to change a member, the compiler realizes that the
value will never make it back to the copy of the struct in the collection
(because what you have is a copy), so you get a compile time error.
Is that pretty close?
Here's a little demo code. I left out some things like the enumerator and
the definition of the Add method, just to try to keep it concise:
public class Class1 :
System.Collections.Specialized.NameObjectCollectionBase
{
public void Load()
{
for(int count = 0; count < 11; count++)
{
TestStruct test = new TestStruct();
test.Processed = false;
test.Property2 = true;
this.Add(count.ToString(), test);
}
}
public void Process()
{
foreach(TestStruct test in this)
test.Processed = true; // causes compile error
}
}
public struct TestStruct
{
public bool Processed;
public bool Property2;
}
going on, but I'd like to understand this a little better.
I have a collection of structs. If I try to iterate the collection of
structs with a foreach() loop, and in the loop I try to change the value of
one of the fields in the struct, I get a compile time error "The left-hand
side of an assignment must be a variable, property, or indexer". (a very
contrived code example at the end of the email)
I _think_ that I understand what the issue is, but I'd like some validation.
I think that the problem is that the collection that I am using uses type
object as its inherent storage type, so when you add a struct to the
collection, it is being boxed implicitly. Then, when you retrieve the
struct from the collection in a foreach, that struct is being implicitly
unboxed. If you try to change a member, the compiler realizes that the
value will never make it back to the copy of the struct in the collection
(because what you have is a copy), so you get a compile time error.
Is that pretty close?
Here's a little demo code. I left out some things like the enumerator and
the definition of the Add method, just to try to keep it concise:
public class Class1 :
System.Collections.Specialized.NameObjectCollectionBase
{
public void Load()
{
for(int count = 0; count < 11; count++)
{
TestStruct test = new TestStruct();
test.Processed = false;
test.Property2 = true;
this.Add(count.ToString(), test);
}
}
public void Process()
{
foreach(TestStruct test in this)
test.Processed = true; // causes compile error
}
}
public struct TestStruct
{
public bool Processed;
public bool Property2;
}