I've read quite a bit about this, but I think it's only gotten me even more confused. To strip down the issue, here's what I want to do:
public sealed partial class MainPage : Page
{
MyFirstDataType Object1 = new MyFirstDataType();
MySecondDataType Object2;
void Button_Click_Event_Handler(Object sender, RoutedEventArgs e)
{
Object2 = new MySecondDataType(Object1);
Object2.DoSomethingUseful();
}
}
Now in the definition for MySecondDataType
:
public class MySecondDataType
{
MyFirstDataType MyObject;
internal MySecondDataType(MyFirstDataType arg)
{
MyObject = arg;
}
public void DoSomethingUseful()
{
//modify MyObject in some way
}
}
So basically I've passed an object to a constructor of another class where I "copy" the object to an internal data member of that class. What I want is that every time the DoSomethingUseful()
function executes, the changes it makes to MyObject
should reflect in the original Object1
on my main page.
From what I've read so far, if I'd simply modified the passed object inside the constructor like arg.counter = 1
, the change would have reflected in the original object too, but the issue arises when I write MyObject = arg;
which creates a separate copy of the object independent from the original. How do I get around this? Or is my understanding of passing by reference flawed? (I have a hunch it is).