Changing C # objects: weird behavior

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].Ge‌​tHashCode() == 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

+6
source share
3 answers

It is difficult to give the correct answer to this, since there is not enough code to correctly develop the problem. However, if you are attached to data, I suppose you need to implement this interface. It is possible that you are a problem, it is simply that you model does not reflect the changes on the screen.

+1
source

I can not reproduce your problem. Here's a simplified view of your relationship to the class (as I understood from your question). Please tell us if this is correct:

 public partial class MainWindow : Window { internal A instanceOfA; internal B instanceOfB; public MainWindow() { InitializeComponent(); instanceOfB = new B() { }; instanceOfA = new A() { listOfBObjects = new List<B>() { instanceOfB } }; } private void Button_Click(object sender, RoutedEventArgs e) { 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(); } } public partial class SecondWindow : Window { internal A instanceOfA; internal B instanceOfB; public SecondWindow() { InitializeComponent(); Loaded += SecondWindow_Loaded; } void SecondWindow_Loaded(object sender, RoutedEventArgs e) { MessageBox .Show(String.Format("{0}", this.instanceOfB == this.instanceOfA.listOfBObjects[0])); this.instanceOfB.three++; this.instanceOfB.four--; } } 

Note: this is not an answer, just an attempt to establish some common ground for further discussions, as comments do not leave you enough freedom for sample code.

+1
source

Thanks to the comments of @ pm_2 and @BillZhang, I found a line in my code where this.currentPropertyToShow was edited. After returning to the first window, infact, I am updating the window, but I do not need to edit this.currentPropertyToShow , so I commented on this and it works! Thank you all for your valuable comments and suggestions!

0
source

Source: https://habr.com/ru/post/950911/


All Articles