0

I am currently trying to add objects(students). I am creating in a foreach loop to an object list made up of students, each list object ends up referencing to the same location and therefore the information on each list object is the same as the last student object created when the loop ends. The only information I have been able to find for adding an object to a list using a foreach loop states to create a new instance of the object which I thought I was doing with the Student stud = new Student line.

I have also tried using the default constructor and using properties instead of arguments, then adding the student to the list however I receive the same result.

Any help in adding objects to an object list with a foreach loop would be greatly appreciated.

foreach loop:

foreach (DataRow datrow in dt.Rows)
{
    Student stud = new Student(
        datrow["ID"].ToString(),
        datrow["fName"].ToString(),
        datrow["lName"].ToString(),
        datrow["GPA"].ToString(),
        datrow["Graduate"].ToString()
    );
    studentList.Add(stud);
}

My Student Constructor:

public Student(
    string studentID,
    string fName,
    string lName,
    string studentGPA,
    string graduate)
{
    iD = studentID;
    firstName = fName;
    lastName = lName;
    gPA = studentGPA;
    grad = graduate;
}

statement used to fill dt

                OleDbCommand cmd = new OleDbCommand(strSql, con);
                con.Open();
                cmd.CommandType = CommandType.Text;
                OleDbDataAdapter dc = new OleDbDataAdapter(cmd);
                dc.Fill(ds, "ID");
                dt = ds.Tables["ID"];
                const char DELIM = ',';
                foreach (DataRow datrow in dt.Rows)

"this" information that each object refers to and is re-written for every new object

Result

19
  • whats wrong with the results? My screen is to small for me to make heads or tails of the picture. Commented Nov 16, 2016 at 14:58
  • 2
    This code looks correct. How have you determined that the problem is here and not somewhere else? Commented Nov 16, 2016 at 14:58
  • Also, in the first screen shot, how can values for this already be populated when you're paused on the first line of the constructor? That seems strange. Commented Nov 16, 2016 at 14:59
  • 1
    Did you check that the rows do not all contain same information? One way to determine if the objects in the list are really all the same or just have same values is to right click and select "generate object id". If they all the same object, they should all get the same id Commented Nov 16, 2016 at 15:01
  • 1
    Max, please include the statement or query that is used to fill the dt object (e.g. Linq or SQL). If all objects contain the same data then probably the rows do too. Commented Nov 16, 2016 at 15:03

2 Answers 2

2

Your private variables are most likely static. This would explain why the variables already have a value in the constructor (which they wouldn't for normal instance variables). The static values are being returned by your properties (which explains how they have values already as well). Change your variables to instance (not static)

Sign up to request clarification or add additional context in comments.

1 Comment

Nice crystal ball :o)
2

Hi my suggestion is just try by declaring class variable outside like this, see whether its works

Student stud;

    foreach (DataRow datrow in dt.Rows)
    {
            stud = new Student(
            datrow["ID"].ToString(),
            datrow["fName"].ToString(),
            datrow["lName"].ToString(),
            datrow["GPA"].ToString(),
            datrow["Graduate"].ToString()
        );
        studentList.Add(stud);
    }

please let me know your suggestions.

thanks karthik

Comments

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.