Why is the parameter is not updated?

I have a class that uses another class.

The first class have this method:

public void myMethod()
{
    //Parameters are an enumeration.
    // Really is a exchange variable between this class
    //and the other class.
    Paramters myParameter = Parameters.Option1;

    MyClass2 myOtherClass = new MyClass2(myParameter);
}

The second class:

public enum Parameters { Option1, Option2, Option3 }

MyClass2
{
    Parameters _myParameters;

    Public MyClass2(Parameters paramParameters)
    {
        _myParameters = paramParameters;
    }

    private void clickButton()
    {
        _myParameters = Parameters.Option2;
        this.Dispose();
    }
}

What I what it is create a dialog and Parameters are an enumeration that is to serve as exchange between the main window and the dialog to notify about the selection in the dialog.

However, when in the clickButton I change the value of the _myParameters, it is not changed in the object that was passed as parameter in the constructor of MyClass2.

If instead of using an enumeration as exchange variable I create a class that has the enumeration, then I can get the selection. The exchange class would be like this:

class MyExchangeClass
{
    Parameters myOption;
}

Then the code would be:

public void myMethod()
{
    //Parameters are an enumeration. 
    // Really is a exchange variable between this class
    //and the other class.
    MyExchangeClass mySelection= new MyExchangeClass();

    MyClass2 myOtherClass = new MyClass2(mySelection);
}

The second class:

public MyExchangeClass
{
    Parameters enum MySelection { Option1, Option2, Option3 }
}

class MyClass2
{
    MyExchangeClass _mySelection;

    Public MyClass2(MyExchangeClassparamParameters)
    {
        _mySelection= paramParameters;
    }

    private void clickButton()
    {
        _mySelection.MySelection = Parameters.Option2;
        this.Dispose();
    }
}

In this way, the Class1, the main window, gets the updated value in the property of the class MyExchangeClass.

I would like to know why in the first solution the enumeration is not updated, because if it would possible, I would like to avoid the needed to wrap the enumeration in a class.

Jon Skeet
people
quotationmark

However, when in the clickButton I change the value of the _myParameters, is not changed in the object that was passed as parameter in the constructor of MyClass2.

No, it wouldn't be. The value was passed in by value - the two variables (myParameter and _myParameters) are independent variables. A change to one variable does not affect the other variable. This is how all types work in C#.

For changes to a parameter within a method to be seen by the caller, you could use a ref parameter, but that's not viable in your case as you're changing an instance variable which was originally populated via a parameter.

You could wrap the value in a mutable class, pass a reference to an instance of that class into MyClass2, and then mutate the object within MyClass2 - that change would be seen within your first class, because that would be changing the data within the object rather than the instance variable of MyClass2. It's hard to know whether or not that's actually a good solution though, as we have so little context - with names like MyClass and myMethod we have no clue as to the bigger picture of what this is trying to achieve.

people

See more on this question at Stackoverflow