A change in one object changes the second one too

我是研究僧i 提交于 2019-12-25 18:35:02

问题


I'm creating two objects and assign them with a data from IsolatedStorage. But when I change one object the second one changes too. ( I think the problem may be the pointers are the same, but I can't solve it. )

private ArrayOfClsSimpleData lstUsers;
private ArrayOfClsSimpleData tmpLstUsers;

in class' globals

tmpLstUsers = IsolatedStorageHelper.GetObject<ArrayOfClsSimpleData>("users");
lstUsers = IsolatedStorageHelper.GetObject<ArrayOfClsSimpleData>("users");

The first status of the arrays:

Debug.Write(lstUsers.Count)

Output: 2

Debug.Write(tmpLstUsers.Count)

Output: 2

The counts are the same as expected. But, after I add an item to one list, the other list gets updated too and the counts are still same.

lstUsers.Add(new ArrayOfClsSimpleData());


Debug.Write(lstUsers.Count)

Output: 3

Debug.Write(tmpLstUsers.Count)

Output: 3


EDIT : IsolatedStorageHelper class is something to help to get objects, save object etc. that I do use for simplifying things, so just think it as getting objects from IsolatedStorage.

it is implemented like this:

 public static T GetObject<T>(string key)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
        {
            return (T)IsolatedStorageSettings.ApplicationSettings[key];  // return the object 
        }

        return default(T); // if key doesn't exists , return default object
    }

So it just gets it from IsolatedStorage. If you don't know isolated storage you can see it from here

So, how can I fix the code so that I can change one without changing the other?


回答1:


So, basically lstUsers and tmpLstUsers are references to the same object. All you have to do is to create a new one and copy content from the original. If you need a quick solution, then you can do it like this (code below). I just guess that ArrayOfClsSimpleData is some kind of array.

lstUsers = IsolatedStorageHelper.GetObject<ArrayOfClsSimpleData>("myKey");
tmpLstUsers = new ArrayOfClsSimpleData();

foreach (object user in lstUsers) // I don't know the type of objects in ArrayOfClsSimpleData, so I wrote 'object', but you should use the actual type
    tmpLstUsers.Add(user);



回答2:


The problem is that IsolatedStorage is just returning two pointers to the same data. So unless you copy the data, all changes will ultimately be to the same underlying data.

Think of it as two copies of your home address. Anything you change on your home affects all copies of your address since it is just an address and not the home itself.

What you will want to do is clone your object. Built in collections have clone or copy methods built in to do shallow copies, or if you built something yourself you will need to implement it yourself

The easiest way is to implement the IClonable interface and to use the clone method to achieve your copying. https://msdn.microsoft.com/en-us/library/System.ICloneable.aspx

This basically involves going through and calling member wise clone for each complex object (which will copy all value types for you) https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx




回答3:


I don't think cloning is necessary. Just create a new list instead of operating on the same instance. You can do that by calling ToList() on the returned instance:

lstUsers = IsolatedStorageHelper.GetObject<ArrayOfClsSimpleData>("myKey").ToList();



回答4:


Can't you use the Clone() method of IClonable while fetching the object? looks like both list objects are getting same reference objects.



来源:https://stackoverflow.com/questions/28764320/a-change-in-one-object-changes-the-second-one-too

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!