1

I'm aware that this would probably never be used in real life, but say I had a bunch of names objects instantiated from the class Student. I.e. I know that the names of my Student objects are "s1, s2, s3" and I want to add these into a List of Students (using a loop), not their fields, but the objects themselves. Again, I want to stress that in general it would never make sense to do this, of course a container would be better. I know this is totally incorrect syntax, but the idea I'm trying to capture is:

Student s1 = new Student(3434,"John Smith");
Student s2 = new Student(5454, "Sam Wilkies");
Student s3 = new Student(7878, "Jim Jam");
List<Student> students= new List<Student>();
for(int i; i<=3; i++){
    string j= "s" + i.ToString();
    students.add(Student[j]);

Like I said I know this is totally incorrect syntax. I thought maybe I could use Activator.CreateInstance (which everyone says to avoid using), but I couldn't figure it out.

Ravmcgav
  • 183
  • 1
  • 1
  • 11
  • Possible duplicate of [string to variable name](https://stackoverflow.com/questions/1293549/string-to-variable-name) – Peter B Oct 26 '18 at 08:00
  • Related or duplicate: [Accessing a variable using a string containing the variable's name](https://stackoverflow.com/q/11122241/1220550) – Peter B Oct 26 '18 at 08:01
  • 1
    ah! i see what you mean now - If you want to get the value of a field based on its `string` name you will have to use `reflection` – jazb Oct 26 '18 at 08:03
  • This might sound ridiculous, but I'm not actually trying to get the value of any particular field. I'm simply trying to add the object _itself_ to a list, by only referencing its name, which I have stored as a string. – Ravmcgav Oct 26 '18 at 08:11
  • 1
    You can't really, not with local variables. Your code boils down to `var students = new List{ s1, s2, s3 }` so there is no need to do that. – nvoigt Oct 26 '18 at 08:11
  • Any time you *number* your variables, you have made the mistake of not using a container (array or list). That's what you should focus on, not how to work around that mistake with duct tape later. – nvoigt Oct 26 '18 at 08:13
  • Ok ok I said at the beginning that this was not for real use, just something someone asked me that I didn't know the answer to do -- this is a time where I was actually curious how to do this specific thing, not about the best way to do it. Of course I would always use a container from the beginning. – Ravmcgav Oct 26 '18 at 08:17
  • It's not clear to me what are you trying to do, it looks like you want a `Dictionary` but not sure. – Alessandro D'Andria Oct 26 '18 at 08:19

1 Answers1

1

The short answer is you cannot. Not for local variables. But that does not mean that you cannot improve your code to do what you want.

Any time you number your variables, you have made the mistake of not using a container (array or list). That's what you should focus on, not how to work around that mistake with duct tape later.

Student[] s = new[] {
    new Student(3434,"John Smith"),
    new Student(5454, "Sam Wilkies"),
    new Student(7878, "Jim Jam")
}

// matter of fact this is not needed now, just to show you the loop:
List<Student> students = new List<Student>();

for(int i = 0; i < 3; i++)
{
    students.Add(s[j]);
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 1
    And the long answer? – Ravmcgav Oct 26 '18 at 08:27
  • 1
    For the long answer you'll have to read the duplicate links to see that a lot can be done with reflection and if you structure your program so that it's not local variables but something else (like properties) it should work. But you will see that it's a lot of work and it's for the use cases where you cannot find another way, not as a duct tape solution to not using the proper language structures in the first place. – nvoigt Oct 26 '18 at 08:29