开发者

How to re-assign a reference type in another class and have changes stick to original?

Here is a rather contrived example of what I am asking:

public partial class Form1 : Form
{
    private Fruit fruit;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        fruit = new Apple();

        Geneticist geneticist = new Geneticist(fruit);

        // Later on in program..

        geneticist.Engineer();

        Console.WriteLine(fruit.Color);

        // Still red because a copy of fruit was made in Geneticist class.
    }
}

class Fruit
{
    public string Color { get; set; }
}

class Apple : Fruit
{
    public Apple()
    {
        Color = "Red";
    }
}

class Banana : Fruit
{
    public Banana()
    {
        Color = "Yellow";
    }
}

class Geneticist
{
    private Fruit fruit;
    private Banana banana;

    public Geneticist(Fruit fruit)
    {
        this.fruit = fruit;
        this.banana = new Banana();
    }

    public void Engineer()
    {
        fruit = banana;
    }
}

Basically, I have a fruit stored as a member variable in my main form. I want to be able to pass it to my Geneticist class and later on have it re-assign the value.

When I type fruit = banana; the fruit in geneticist no longer points to the the Form1 fruit but 开发者_开发百科instead to the local copy in Geneticist. I am looking for a way to simulate the ref keyword, I suppose, where if I re-assign the geneticist fruit the Form1 fruit is also updated with the change.

I suppose I could create a wrapper of fruit and pass that around instead but that seems a bit hackish. Also I could have the Engineer method raise an event so that the main form can re-assign the value but having to do that at many parts of my program seems a bit messy as well.

Also, I cannot use the ref keyword because I modify it later on, not in the constructor of Geneticist.

Thanks for reading!


The problem with this is that if I re-assign it, it doesn't affect the original in Form1. A copy is made in Geneticist when I type fruit = banana.

No, this is wrong. No copy is made.

What happens is that you overwrite the reference you have at hand of the Apple with a reference to a Banana. It's as if you are given an apple to put in your pocket and after holding on to it for a little while, you leave it on the ground, pick up a banana, and place it in your pocket.

When at some later point you decide to eat the original apple will be intact, but not because you made a copy. Only because you just lost interest in it and got hold of a totally unrelated fruit instead.

So what to do?

The fact remains that you cannot modify the Fruit parameter as a whole with reference semantics (the CLR does not allow refs to be stored as class members, as you yourself have said).

If you want Geneticist to modify the Fruit reference, then you do have to create a wrapper around it. But the most practical solution would be to have the calling code cooperate with Geneticist:

class Geneticist
{
    private Fruit fruit;
    private Banana banana;

    public Geneticist(Fruit fruit)
    {
        this.fruit = fruit;
        this.banana = new Banana();
    }

    public Fruit Engineer()
    {
        fruit = banana;
        return fruit; // return the new value
    }
}

And the calling code:

fruit = new Apple();

Geneticist geneticist = new Geneticist(fruit);
fruit = geneticist.Engineer(); // use the return value this way
Console.WriteLine(fruit.Color);

Another workable approach

What about letting the Geneticist know how to modify the fruit themselves?

class Geneticist
{
    private Fruit fruit;

    private readonly Banana banana;

    private readonly Action<Fruit> engineer;

    public Geneticist(Fruit fruit, Action<Fruit> engineer)
    {
        this.fruit = fruit;
        this.banana = new Banana();
        this.engineer = engineer;
    }

    public void Engineer()
    {
        this.engineer(this.banana);
    }
}

And the calling code:

Fruit fruit = new Apple();
Geneticist geneticist = new Geneticist(fruit, f => { fruit = f; });
geneticist.Engineer();
Console.WriteLine(fruit.Color);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜