FrancescoDS
FrancescoDS

Reputation: 995

c# objects modification: a strange behaviour

I'm developing a WPF C# application and I have a strange behaviour in modification of objects. I try to explain it in general way. Suppose that you have an object of a class described as follows:

public class A
{
  int one;
  bool two;
  List<B> listofBObjects;
}

where B is:

public class B
{
  int three;
  int four;
}

I pass an instance of A class and an instance of B class from a window to another, only defining two variables of type A and B in the second window and passing them before the Show() method, with the following code, executed into an instance of window FirstWindow:

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 have to repeat this code twice(that is, opening twice the window), in the first execution everything works as expected, infact if I modify values in instanceOfB variable, I see the modification also in instanceOfA variable. But, in the second execution, the modification in instanceOfB does not affect instanceOfA... The modifications are done in newWindow. For example:

this.instanceOfB.three++;
this.instanceOfB.four--;

Imagine that you are in the FirstWindow. Click on a button and SecondWindow opens, passing both variables as described above. In SecondWindow, do some modifications, click on OK and SecondWindow closes, returning control to FirstWindow. If I reclick on the same button, I reopen SecondWindow. If I do modifications now, they do not affect both variables.

I try to have a look (in VS2012) at both variables in the console with control expression and I see that, in the first pass of code, both variables changes when code above is executed but, in the second pass of code, only instanceOfB changes...

EDIT: Following the code that I use to pass parameters to SecondWindow...types are explaind 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();

Consider that obj is given by a button selection into a datagrid, in which each row represents an IntermediatePosition object. In the datagrid, there is a column button and, clicking by buttons, IntermediatePositionsSettingsWindow is opened with the proper data

EDIT: I've performed the folloqing check:

this.currentPropertyToShow.sides[this.sideIndex].intermediatePositionList[i].Ge‌​tHashCode() == obj.GetHashCode()

where i is the index of related IntermediatePosition object. At first usage of IntermediatePositionsSettingsWindow the objects result equals, but in second usage they are different

Why this thing happens? If it is needed any other clarification, I will edit the question Thanks

Upvotes: 6

Views: 382

Answers (3)

FrancescoDS
FrancescoDS

Reputation: 995

Thanks to @pm_2 and @BillZhang comments, I found a row in my code in which this.currentPropertyToShowwas edited. After the returning back at first window, infact, I perform the refresh of the window, but it is not needed to edit this.currentPropertyToShow, so I have commented it and everything works! Thanks everybody for precious comments and suggestions!

Upvotes: 0

Paul Michaels
Paul Michaels

Reputation: 16695

It's difficult to give a proper answer to this, as there is insufficient code to correctly work out the issue. However, if you are databinding, then I believe you need to implement this interface. It is possible that you're issue is simply that you're model is not reflecting the changes to the screen.

Upvotes: 1

Alex Filipovici
Alex Filipovici

Reputation: 32561

I can't reproduce your problem. Here's a simplified representation of your class relation (as I understood from your question). Please let us know 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 trying to establish some common ground for further discussions, as comments don't leave you enough freedom for code samples.

Upvotes: 1

Related Questions