G
Guest
I am getting this error when I tried to modify one field inside foreach loop.
public struct myStruct
{
public int a;
public bool b;
//...
}
private List<myStruct> MyStruct = new List<myStruct>();
//...
foreach (myStruct ms in MyStruct)
{
if (ms.a == 3)
{
ms.b = true; // Can't modify members because... ?
}
}
So I tried below code that could be compiled anyway
foreach (myStruct ms in MyStruct)
{
if (ms.a == 3)
{
mtStruct copy = ms;
copy.b = true; // compiled successfully..
}
}
What would you do in this case?
Thanks
Bob
public struct myStruct
{
public int a;
public bool b;
//...
}
private List<myStruct> MyStruct = new List<myStruct>();
//...
foreach (myStruct ms in MyStruct)
{
if (ms.a == 3)
{
ms.b = true; // Can't modify members because... ?
}
}
So I tried below code that could be compiled anyway
foreach (myStruct ms in MyStruct)
{
if (ms.a == 3)
{
mtStruct copy = ms;
copy.b = true; // compiled successfully..
}
}
What would you do in this case?
Thanks
Bob