I have a problem with one gimmick that I would like to try and make it work.
I would like to have a class in a separate CS file, that already has a few instances of it defined in itself and a List of those instances availible for reference in Main. Is it possible? Should I use a nested class for it? Or else, what would be the best way to do it?
If this is going to work I will never have to edit this class except to add more instances. I just don't want to instantiate this class in Main because I don't want to accidentially change some values there, by some missclick etc. I just want to have this class in a separate file and to never look at it again, just referencing the list that is already there.
What I have tried:
class Parent
{
private static List<Nested> list_of_instances = new List<Nested>();
readonly Nested instance1 = new Nested { parameter1=123, parameter2=1234};
readonly Nested instance2 = new Nested { parameter1=321, parameter2=4321};
public static List<Nested> List_of_instances
{
get { return list_of_instances; }
}
class Nested
{
public double parameter1 { get; set; }
public double parameter2 { get; set; }
public Nested()
{
list_of_instances.Add(this);
}
}
}
And in Main I want to obtain the list of those default instances like this:
List<double> parameter1_list = new List<double>();
foreach (Parent.Nested item in Parent.List_of_instances)
{
parameter1_list.Add(item.parameter1);
}
ComboBox1.ItemsSource = parameter1_list;
In XAML I have just a simple ComboBox that I want to populate with values of parameter1 of every class instance:
<ComboBox x:Name="ComboBox1"/>
In the end, nothing is added to the combo box and I get no errors in this program so I don't know where to start looking for my mistake.
Is this whole concept just wrong or is this doable? What would be the correct way for getting this do work?