0
var a = new myTestClass();
var b = new myTestClass2();
list<string> instList = new list<string>();
instList.add("b");

public void simpleFunc()
{
foreach(string i in instList){
a.fieldName = **i.myFieldName;**  
//HERE i is b which refers to the instance of myTestClass2 
}

what I would like to accomplish here is: create a List of string whihc are the names of class instances then inside a for each loop use the instlist strings like an instance of the class and add something to that property of that class

is this possible?

Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42

1 Answers1

2

If you really want to do that, you can create a dictionary with the name as key and the instance as value. I do wonder though why you are thinking the variable name is so important... A variable name can change easily and after all, it is just a pointer to the actual object. Shouldn't you create a class to save the 'metadata' of the variable?

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add(nameof(a), a);
d.Add(nameof(b), b);

string valueAtB = d["b"];
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325