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.

8
  • Possible duplicate of string to variable name Commented Oct 26, 2018 at 8:00
  • Related or duplicate: Accessing a variable using a string containing the variable's name Commented Oct 26, 2018 at 8: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 Commented Oct 26, 2018 at 8: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. Commented Oct 26, 2018 at 8:11
  • 1
    You can't really, not with local variables. Your code boils down to var students = new List<Student>{ s1, s2, s3 } so there is no need to do that. Commented Oct 26, 2018 at 8:11

1 Answer 1

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]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

And the long answer?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.