Color is a structure in C#, which is copied during assignments. That means when you assign:
clr[0,0] = form1.btn1Color;
It assigns a copy of the value of form1.btn1Color to clr[0,0]. Thus, when you then say:
clr[0,0] = Color.Aqua;
You are overwriting the copy, not the original.
In fact, even if Color were a class instead of struct the result would be the same, because in that case you'd overwrite the reference.
If you wanted assigning the array to modify the original object, you would need to wrap the object in the array to refer back to the original object that has the member holding the Color, since Color is an immutable struct.
Update: based on your follow up question, one thing you could do (there are really many ways to do this) is to wrap the Color in a wrapper class, and use that wrapper for the array and as the member in form1 as well:
public class ColorHolder
{
// construct ColorHolder from Color
public ColorHolder(Color color)
{
Current = color;
}
// can assign ColorHolder to Color with implicit conversion
public static implicit operator Color(ColorHolder value)
{
return value.Current;
}
// get or set current color
public Color Current { get; set; }
}
Then, change form1.btn1Color to be ColorHelper instead of Color and this works:
ColorHolder[,] clr = new ColorHolder[50, 50];
// sets current color of btn1Color to black.
form1.btn1Color = new ColorHolder(Color.Black);
// assign array location to the color holder
clr[0, 0] = form1.btn1Color;
// this changes the current color of form1's btn1Color
clr[0, 0].Current = Color.Aqua;
Note: You could also allow a conversion from Color -> ColorHolder as well, but this could lead to hard-to-track bugs as it would allow:
clr[0,0] = Color.Aqua;
Which would assign a new ColorHolder instead of changing the existing using Current. Thus I'd avoid the Color -> ColorHolder conversion and stick with something like the above.
You could even generalize this Holder code to support any type, like:
public class Holder<T>
{
public Holder(T value)
{
Value = value;
}
public static implicit operator T(Holder<T> holder)
{
return holder.Value;
}
public T Value { get; set; }
}
Then instead of ColorHolder you'd have Holder<Color> or any other type. Again, this would be used like the following (assuming form1's btn1Color member is now Holder<Color>):
Holder<Color>[,] clr = new Holder<Color>[50, 50];
form1.btn1Color = new Holder<Color>(Color.Black);
clr[0, 0] = form1.btn1Color;
// sets the Color in form1.btn1Color to Aqua...
clr[0, 0].Value = Color.Aqua;
Is there a C# limitation of setting form properties from an array?- TBH, this wouldn't be my first guess...