Objects inside a class getting overwrited?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scalp994
    New Member
    • Feb 2012
    • 8

    Objects inside a class getting overwrited?

    Hello,
    I've encountered a problem.

    I've a class that looks like this:
    Code:
    public class student
            {
                public int number;
                public string surname;
                public string[] subjects = new string[10];
            };
    The objects of the class are then added to a list
    Code:
    List<student> students = new List<student>();
    Code:
    students.Add(new student
                {
                    number = counter1,
                    surname = textBox1.Text,
                    subjects = choice
                });
    while
    Code:
    choice[0] = listView1.Items[counter1].SubItems[1].Text;
                choice[1] = listView1.Items[counter1].SubItems[2].Text;
    etc.

    My problem is that, when I add two students, and let's say, I set English on Higher Level on the first one and on Standard Level on the second one, the choice of the first one is getting overwrited. As such, both take the value "Standard Level". Any ideas on how may I fix this? May I somehow lock the items inside the list to make them non-changeable upon addition?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    The thing to remember about C# is that almost everything is a reference type. In fact, the only things that aren't are the primitive types, such as int, double, float, string, bool, and struct. Anything else, the instantiation of a class, an array, etc... is a reference. Meaning all the variable holds is an address to allocated memory.

    So in your case, your subjects member in your student object is a string array. When you create a new student, you assign in to an existing string array (thus overwriting what you initialized). Now your student's subject member points to the choices object. If you create two students and assign both their subjects to choices, both of those students have their subjects looking at the same object. You then modify the choices object which changes both student objects.

    Hopefully that makes sense.

    Anyway, you need to make sure that students contains it's old objects if you want it to maintain a unique list of subjects. This means you'll need to copy the choice data... My recommendation would be to change the subjects type to a List<string> instead of a fixed size string array. Then you can add each item in your choice array into that, creating a copy.

    Code:
    public class Student
    {
      ...
      private List<string> m_subjects = new List<string>();
    
      public Subjects { get { return m_subjects; } }
      ...
    }
    Code:
    Student newStudent = new Student()
    {
      Students.AddRange(choice)
    };
    students.Add(newStudent);
    Note that the AddRange method on a List<T> takes a T[] (T stands for any type) parameter, which is an array. This is equivalent to doing...

    Code:
    for (int i = 0; i < choice.length; i++)
      newStudent.Subjects.Add(choice[i]);

    Comment

    Working...