I have problem with store data in list collection. If I add new data it rewrite old data, and in list is still only one item.
Here is main method, from this method I call method OpenChatScreen, It’s method of ChatScreenManager class where is root of problen.
private void OpenTabChatWindow(string nick)
{
try
{
new System.Threading.Tasks.Task(() =>
{
IDetailData oponent = new DetailData();
oponent = Service.DetailData(Account, nick);
Execute.OnUIThread((System.Action)(() =>
{
//here I call problem method OpenChatScreen method where is the problem,
//it use still the same reference on object opponent
if (ChatScreenManager.OpenChatScreen(true, Account, oponent, Account.DetailData.Info.Nick))
{
AddConversationHistory(nick);
}
}));
}
).Start();
}
catch (Exception exception)
{
MsgBox.ShowException(exception);
}
}
Code from ChatScreenManager class:
public IDictionary<string,object> ActiveChatScreens { get; set; }
or
public IList<string,> ActiveChatScreens { get; set; }
Problem is same if I use dictionary or list.
public bool OpenChatScreen(bool useTabChat, IAccount account, IDetailData oponent, string avatarNick)
{
if (!ActiveChatScreens.Contains(oponent.Info.Nick))
{
if(useTabChat)
{
//in this method - OpenTabChat is problem
OpenTabChat(account, oponent, avatarNick);
return true;
}
}
return false;
}
private void OpenTabChat(IAccount account, IDetailData oponent, string avatarNick)
{
if (!ChatShellViewModel.IsActive)
{
OpenChatShell();
}
ChatShellViewModel.OpenChatTab(account, oponent, avatarNick);
//here is the root of problem, it use same reference of object opponent
ActiveChatScreens.Add(oponent.Info.Nick);
}
So I pass from method OpenTabChatWindow object type of DetailData and store som string property in List in another class, but is use same reference on this object and rewrite data in list.
I try create new insatce of object:
IDetailData oponent = new DetailData();
oponent = Service.DetailData(Account, nick);
And pass this object to problem method, but it didn’t solve it.
Now if I understand your examples.
You add objects to ActiveChatScreens.
Could it be that you are reusing the same object in the call to OpenChatScreen in the first place.
Id so then opponent will change and any reference to it will also change before these methods is even called?
Remember objects are reference structures, even if you change the data in them, the reference is still the same.