Assign a variable name to new object

nabilg

New member
Joined
Oct 21, 2019
Messages
2
Programming Experience
10+
Hello,
I would like to know if it is possible to assign a variable name that I defined according to another name to create an object (example DataTable, ArrayList, etc ...)?
example:
string myvar = "my_list";
ArrayList myvar = new ArrayList();

Thank you.
 
I'm not sure if this is what you're asking but if it is, you can use nameof :
C#:
            var xver = 0;
            var name = nameof(xver);
If you want to have the two in the same class, then no. You would need to have one of them in a separate class.
 
Thank you Sheepings for your replay. I'm going to explain you:
I have several DataTables ( table1, table2, ....). I want to create correspondant ArrayLists with a do while........
the ArrayLists will have the same names but with AL_tables1, AL_table2..............
That means I would like to name the ArrayLists with a "AL_"+DataTable.Name
Best regards.
 
If you need a string lookup on a collection you would generally use a Dictionary, for example Dictionary<string, ArrayList> or similar.
Also, in general you would never use a ArrayList these days, you would use a typed List, for example List<object> or more specific List<string>.
 
Back
Top Bottom