Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have user-defined class contains three public string type variables. I am creating its list so as to add as many items of that type. but on adding list.Add(item) list is over writing last item to each of them. following code


public class QuizSearchListVariable{

public string quiz_type_id;
public string quiz_type_title;
public string quiz_type_description;
public void Clear()
{
quiz_type_id = string.Empty;
quiz_type_title = string.Empty;
quiz_type_description = string.Empty;
}

public override string ToString ()
{
return string.Format ("[QuizSearchListVariable] \n Quiz_Type_ID={0} \n Quiz_Type_Title={1} \n Quiz_Type_Description={2}", quiz_type_id, quiz_type_title, quiz_type_description);
}

What I have tried:

/ Adding items from here

for (int i = 0; i < data ["quiz_list"].Count; i++) {
Obj1.Clear ();
Obj1.quiz_type_id = (string)data ["quiz_list"] [i] ["id"];
Obj1.quiz_type_title = (string)data ["quiz_list"] [i] ["title"];
Obj1.quiz_type_description = (string)data ["quiz_list"] [i] ["description"];
MyQuizList.Add (Obj1);
QuizTitleList.Add (data ["quiz_list"] [i] ["title"].ToString ().ToLower ());
Posted
Updated 5-Aug-16 3:25am

Your code is using one instance of the Class, 'Obj1, repeatedly, and adding that same instance to the List. Yes, you clear the fields of that instance of 'Obj1, and write new values, but, the reference to 'Obj1 stays the same; at the end, you have multiple references to 'Obj1 in your List 'MyQuizList, and all of them contain the last values you wrote.

So, you need to create a new instance of your Class with each iteration of the loop:

Obj1 = new QuizSearchListVariable();
 
Share this answer
 
v3
you keep passing the same referenced object to the list. need to create a new object every time and populate it with the new fields, otherwise the previous objects will have the new values since it's only a reference.
 
Share this answer
 
You need to understand how reference variables work

C#
for (int i = 0; i < data ["quiz_list"].Count; i++) {
    Obj1.Clear ();
    // ...
    MyQuizList.Add (Obj1);

There is only one copy of Obj1, and you are clearing its data, setting its data and adding it to the list, however you are only adding the same object multiple times, you don't have different copies of the item.

Imagine you have a bag and inside that bag you put "square", and you get someone to hold that bad. You then clear the bag so you remove square and you put "circle" in it and tell someone else to hold the bag. So you have one bag that both people are holding and that bag holds a circle.

What you really want to do is create a new bag rather than clear your existing one, so you have one bag with a square and one with a circle. So replace Clear with creating a new instance.

C#
for (int i = 0; i < data ["quiz_list"].Count; i++) {
    Obj1 = new QuizSearchListVariable();
    // ...
    MyQuizList.Add (Obj1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900