0

How I can create Objects of MyClass Dynamically? I have nested lists, I will get the count of nested lists from my DataTable, so I don't know how many objects I need. I am working inside loop.

Can I please get some help with example?

for (int i = 0; i < table.Rows.Count; i++)
{
List<MyClass> names[i] = new List<Checklists>();
}

So I will get:

names0
names1
names2
......n

And then I will Add elements like

name0.Add(new MyClass(...))
name1.Add(new MyClass(...))
name2.Add(new MyClass(...))

.........

I read a few questions on StackOverFlow, but they are not helping in my case, as they are not about List. Like Creating objects dynamically in loop

Community
  • 1
  • 1
Khaksar
  • 333
  • 3
  • 17
  • You cant do that dude. You dont want dynamic objects, you want dynamic variables. – Stormhashe Nov 01 '16 at 19:17
  • Can you [edit] your question to give a full [mcve] for your problem? It sounds like [`SelectMany()`](https://msdn.microsoft.com/en-us/library/bb534336.aspx) with [`ToList()`](https://stackoverflow.com/questions/7617771/converting-from-ienumerable-to-list) might do the job. – dbc Nov 01 '16 at 19:23
  • 1
    It really looks to me like what you may need is a `Dictionary>` or something to that effect. The key would be what you use to access the list for that name and add your class to it. Obviously you would also need to find a way to prevent issues if two people have the same name but hopefully this gave you enough of a hint to point you in the right direction... – HDL_CinC_Dragon Nov 01 '16 at 19:25
  • Are you wanting one instance of MyClass for each row in the table or are you wanting an actual List of MyClass for each row? If you want the List then how should each instance of MyClass be created from one row in the table? It sounds like you might want List> in the end. – Blake Thingstad Nov 01 '16 at 19:33

1 Answers1

1

You can create a class say e.g. LISTOFNAMES that can have a public member names[] to be an array of

          List <MyClass> 

type. The size of the array can be passed as an integer variable to the constructor of LISTOFNAMES(..) and at your code you can instantiate each names[i] as an individual List. The following example code shows the described way

     // Add a public class LISTOFNAMES

       public class LISTOFNAMES
       {
        public List<MyClass>[] names;
        public LISTOFNAMES(int arsize)
        {
             this.names=new List<MyClass>[arsize];
        }
       }

Then at your code you have to instantiate at first an object of type LISTOFNAMES (supposing you have acquired the number of elements you need for the array names[index]) and then you have to instantiate each names[index] individually to be of

       List <MyClass> 

type. Thus, you can add the following lines

       //in your code
       //suppose you have got a value for the number of names[] items say
      // to be named nn (of type int), then you add the following

       LISTOFNAMES lista=new LISTOFNAMES(nn);
       // Then you add..
       for(int i=0;i<nn;i++)
       {
          lista.names[i]=new List<MyClass>();
       }
       //Now you can use individually the lista.names[index] objects,
      // for example like
       lista.names[0].Add(new MyClass(...));
       lista.names[1].Add(new MyClass(...));

       etc

Alternatively, you can simply instantiate an array object of type

      List<MyClass>[]

and then instantiate each item separately. e.g Having acquired the array size to the "ar_size" being an int variable e.g.

                 int ar_size=table.Rows.Count;

you can use the following

          List<MyClass>[] names=new List<MyClass>[ar_size];
          for(int i=0;i<ar_size;i++)
             {
                names[i]=new List<MyClass>();
             }

And then fill each object names[index] , like the following

            names[0].Add(new MyClass(...));
            names[1].Add(new MyClass(...));

       etc.

Hope these help.

SteveTheGrk
  • 352
  • 1
  • 7