0

I store data in Session["miData"], I want to return this object but remove certain items in a list within the object.

This is my code for this:

else
{
    ListDevicesByLabelModel data = (ListDevicesByLabelModel)Session["miData"];
    ListDevicesByLabelModel tempdata = data;
    if (filterType == "model" && filterKey != null)
    {
        tempdata.devices.device = data.devices.device.Where(c => c.model == filterKey).ToList();
    }
    return Json(tempdata, JsonRequestBehavior.AllowGet);
}

However the actual object in Session["miData"] is changed by this code.

What am I doing wrong?

ekad
  • 14,436
  • 26
  • 44
  • 46
Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109
  • `ListDevicesByLabelModel tempdata = data;` does not create a copy of data. it just creates a second variable that refernces the same object as the first. However `ListDevicesByLabelModel data = (ListDevicesByLabelModel)Session["miData"];` creates a copy of the item stored in the sessionvariable so if you do `data.someProp = somevalue` it should not change the object in the session variable. – Ben Robinson Dec 12 '14 at 15:42

1 Answers1

1

When you get objects from Session you don't need to worry about your object being modifed, because when you get the object from Session a copy is already created for you. So your code in fact is redundant.

Here's some general info on how you copy an objects that are passed by reference.

Objects are assigned by reference, which points to the same object. So when you are assigning one variable to another it doesn't copy the object itself.

You need to do a Copy of the object by implementing IClonable interface in your class or creating a method which will return a new object with fields copied from existing object like this (this will not work if you have nested objects, if that's the case you need to do a deep copy: How do you do a deep copy of an object in .NET (C# specifically)?) :

public class  ListDevicesByLabelModel
{ 
     public ListDevicesByLabelModel Clone()
     {
          var newObj = new ListDevicesByLabelModel();
          newObj.SomeProperty = SomeProperty;
          //assign other properites
          return newObj;
     }
}

And use it later:

ListDevicesByLabelModel data = (ListDevicesByLabelModel)Session["miData"];
ListDevicesByLabelModel tempdata = data.Clone();
if (filterType == "model" && filterKey != null)
{
     tempdata.devices.device = data.devices.device.Where(c => c.model == filterKey).ToList();
}
return Json(tempdata, JsonRequestBehavior.AllowGet);
Community
  • 1
  • 1
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
  • public ListDevicesByLabelModel Clone() { var temp = new ListDevicesByLabelModel(); temp.totalCount = totalCount; temp.messages = messages; temp.devices = devices; return temp; } still same issue – Lord Vermillion Dec 12 '14 at 15:48
  • @TobiasOlofsson you don't need to worry about this at all as you `Session` won't be modified if you modify your data after retrieving it. As for your case you need to do `Deep Copy` which will care about nested objects too (you can check this question: http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically ) – Vsevolod Goloviznin Dec 12 '14 at 15:50