I am developing a WPF C # application and I have strange behavior when modifying objects. I try to explain this in general. Suppose you have a class object described below:
public class A { int one; bool two; List<B> listofBObjects; }
where B:
public class B { int three; int four; }
I pass the instance of class A and the instance of class B from the window to another, defining only two variables of type A and B in the second window and passing them before the Show () method with the following code executed in the instance of the FirstWindow window:
SecondWindow newWindow = new SecondWindow(); newWindow.instanceOfA = this.instanceOfA; //instanceOfA is of type A newWindow.instanceOfB = this.instanceOfA.listOfBObjects[0]; //instanceOfB is of type B newWindow.Show();
If I need to repeat this code twice (that is, opening the window twice), in the first run everything works as expected, infact, if I change the values in the instanceOfB variable, I see the modification also in the instanceOfA variable. But in the second execution, the modification in instanceOfB does not affect instanceOfA ... Changes are made in newWindow . For instance:
this.instanceOfB.three++; this.instanceOfB.four--;
Imagine that you are in FirstWindow. Click the button and the SecondWindow window opens, passing both variables as described above. In SecondWindow, make some changes, click OK and SecondWindow will close, returning control to FirstWindow. If I lean back on the same button, I will open SecondWindow again. If I make changes now, they do not affect both variables.
I try (in VS2012) to look at both variables in the console using a control expression, and I see that in the first pass of the code, both variables change when the code above is executed, but in the second pass of the code, only instanceOfB changes ...
EDIT: Following the code that I use to pass parameters to SecondWindow ..., the types are explained below
IntermediatePosition obj = ((FrameworkElement)sender).DataContext as IntermediatePosition; //IntermediatePosition is Class B IntermediatePositionsSettingsWindow ips = new IntermediatePositionsSettingsWindow(); ips.currentIntermediatePosition = obj;//this is the instanceOfB ips.idxOfIpToModify = obj.index; ips.currentSingleProperty = this.currentPropertyToShow; //this is the instanceOfA object ips.sideIndex = this.sideIndex; ips.ShowDialog();
Note that obj is set by selecting a button in a datagrid in which each row represents an IntermediatePosition object. There is a column button in the datagrid and, by clicking the button, IntermediatePositionsSettingsWindow opens with the corresponding data
EDIT: I performed the following check:
this.currentPropertyToShow.sides[this.sideIndex].intermediatePositionList[i].GetHashCode() == obj.GetHashCode()
where i is the index of the associated IntermediatePosition object. The first time you use IntermediatePositionsSettingsWindow result of the object is equal, but in the second use, they differ
Why is this happening? If any other clarification is needed, I will edit the question Thanks