Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
Posted
Updated 4-Sep-18 7:34am

The overall approach is not very recommended but to answer your question you need to do certain changes in your code. Below are the changes that you need to do in your parent class which should solve your problem.

public class Nested
{
        private static Nested instance1 = new Nested { parameter1 = 123, parameter2 = 1234 };
        private static readonly Nested instance2 = new Nested { parameter1 = 321, parameter2 = 4321 };
        private static List<Nested> list_of_instances = new List<Nested>();
        public static List<Nested> List_of_instances
        {
            get
            {
                if (list_of_instances == null)
                {
                    list_of_instances.Add(instance1);
                    list_of_instances.Add(instance2);
                }
                return list_of_instances;
            }
        }
        public double parameter1 { get; set; }
        public double parameter2 { get; set; }

        private Nested()
        {
            //Private constructor will make sure that no other class able to make instance of this object
        }
}


You can now loop through your instance list in Main method:-

List<double> parameter1_list = new List<double>();
            foreach (Nested item in Nested.List_of_instances)
            {
                parameter1_list.Add(item.parameter1);
            }

            ComboBox1.ItemsSource = parameter1_list;
 
Share this answer
 
Well I will answer my own question...

To instantiate a class in itself all I had to do was to add a "static" keyword for every new instance.

That was all. No need for nested classes.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 2-Sep-18 12:51pm    
You are aware of the fact that static and instance, these terms, are total opposite of each other.

By the way, can you show how you solved the problem?
Member 13846376 4-Sep-18 13:35pm    
Well, no, these terms are not "total opposite of each other" they are just two different things. But I know what you mean, when you think "static" you think "only one instance possible", but if you set each instance static then each of them exist only once. Makes sense.
    class Class1
        {
            public string Parameter1 { get; set; }
            public double Parameter2 { get; set; }


            public static List<Class1> List_class1 { get; } = new List<Class1>();
            public static List<string> list_parameter1 = new List<string>();


            static Class1 B20 = new Class1 { Parameter1 = "name1", Parameter2 = 20 };
            static Class1 B25 = new Class1 { Parameter1 = "name2", Parameter2 = 25 };


            public Class1()
            {
                List_class1.Add(this);

            }

            public static List<string> Nazwy()
            {
                foreach (Class1 bet in Class1.List_class1)
                {
                    list_parameter1.Add(bet.Parameter1);
                }
                return list_parameter1;
            }
        }
    }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900