-2

In c# why is it not allowing to create variable names dynamically? I am trying something like this:

 for (int i = 0; i < ceoList.Count; i++)
 {
      List<VP> "vp" + i = new List<VP>();                   
 }

I want to generate variable names at run time. Like "vp" + i here.

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
Praveen
  • 166
  • 2
  • 4
  • 13

2 Answers2

2

You don't want to create variable names at run time, you want to create data structures.

 List<List<VP>> vps = new List<List<VP>>

 for (int i = 0; i < ceoList.Count; i++)
            {
                vps.Add( new List<VP>());
            }
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
0

What you are trying to accomplish is not a valid programming syntax, and will not compile in any imperative programming language compiler!

What you want is a way to name references to a set of objects of the type List, so to achieve that I would recommend you to check it out HashTables in the namespace System.Collections.

Hashtable vpTable = new Hashtable();

for (int i = 0; i < ceoList.Count; i++)

{

vpTable.add("vp" + i, new List());

}

Community
  • 1
  • 1
João Pinho
  • 3,725
  • 1
  • 19
  • 29