0

As the title says, I'm trying to take an item from one list of objects add it to another list and delete the item from the original list without it being deleted from my new list.

This is what I've tried so far:

            _ToUpdateQnA.Add(_qnaPair[rowIndex]);

            int delIndex = Int32.Parse((e.CommandArgument).ToString());

            ListViewDataItem item = (e.CommandSource) as ListViewDataItem;
            TextBox deletedQtn = (TextBox)item.FindControl("QuestionTb");
            string delQtn = deletedQtn.Text;

            _qnaPair[rowIndex].Questions[delIndex].delta_question_description = delQtn;
            _qnaPair[rowIndex].DelQuestions.Add(delQtn);
            
            _qnaPair[rowIndex].Questions[delIndex].status = "toDelete";

            _qnaPair[rowIndex].Questions.RemoveAt(delIndex);

After this code runs, _ToUpdateQnA keeps the _qnaPair object however, deletes the '_qnaPair[rowIndex].Questions[delIndex]' which has now had it's status updated to "toDelete" which I'd like to remain in the _ToUpdateQnA List.

Questions is a list of objects within a QnAPair object.

Thanks I'm quite new at this.

thin580
  • 1
  • 2
  • What you need is a `deep copy` of the object you copy. This might help. https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net – Sach Jul 07 '20 at 22:16
  • Tip: You should never use the Text property to keep track of anything, instead use the Tag property to store something you would like to track. For example an Id or an object. You can always typecast the Tag to anything you stored on – Jeroen van Langen Jul 07 '20 at 22:20
  • `_qnaPair[rowIndex]` is a *reference* type, so when you add it to `_ToUpdateQnA`, you're adding a reference to the object. So if you delete something from its `Questions` list, in one place, it will be reflected in all instances that refer to the same place. If you don't want this behavior, then you'll need to create a deep copy of the item and add *that* to `_ToUpdateQnA`. – Rufus L Jul 08 '20 at 19:07

0 Answers0