Lets say I have the class
class Foo
{
public int Value { get; set; }
}
And I have the global variables:
public static Foo A1 { get; set; }
public static Foo A2 { get; set; }
public static Foo A3 { get; set; }
public static Foo A4 { get; set; }
// etc... more
The following code will not set the value of those global variables:
var elementsToSet = new Foo[]{A1,A2,A3,A4};
for (var i = 0; i < elementsToSet.Length; i++)
elementsToSet[i] = new Foo {Value = i};
In other words A1 == null is true :(
I am trying to initialize A1, A2, etc not the array
how can I prevent having to do:
A1 = new Foo { Value = 1 };
A2 = new Foo { Value = 2 };
A3 = new Foo { Value = 3 };
A4 = new Foo { Value = 4 };
// etc...
PS
I know I can have an array as my global variable instead of several. I am just asking this question to learn. I have the reference of each variable. I want to set a new variable of the same type at that address.
Also
If Foo will have been a struct instead of a reference type (class) this will work no?