Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6571383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:52:31+00:00 2026-05-25T14:52:31+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:52:32+00:00Added an answer on May 25, 2026 at 2:52 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems rather common (around here, at least) for people to recommend SVN to
The PHP documentation can be found here , but I think it's rather lacking.
Here's a problem I ran into recently. I have attributes strings of the form
Here is my code, which takes two version identifiers in the form 1, 5,
I have code similar to the following: public class myButton extends JButton() { public
I've been following with great interest the converstaion here: Construct Query with Linq rather
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.