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!
No, this is wrong. No copy is made.
What happens is that you overwrite the reference you have at hand of the
Applewith a reference to aBanana. 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
Fruitparameter 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
Geneticistto modify theFruitreference, then you do have to create a wrapper around it. But the most practical solution would be to have the calling code cooperate withGeneticist:And the calling code:
Another workable approach
What about letting the
Geneticistknow how to modify the fruit themselves?And the calling code: