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

  • Home
  • SEARCH
  • 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 747007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:09:21+00:00 2026-05-14T14:09:21+00:00

I have a confusion that when i pass a variable by refrence in the

  • 0

I have a confusion that when i pass a variable by refrence in the constructor of another class and after passing that object by refrence i recreate the refrence object with the new keyword.

Now the class in which i have passed the refrenced object dosen’t reflect the updated data.
An exabple of the above problem is shown below:

Object to be passed by Refrence:

public class DummyObject
{
    public string Name = "My Name";

    public DummyObject()
    { }
}

Class which is passing the Refrence:

public partial class Form1 : Form
{
    // Object to be passed as refrence
    DummyObject dummyObject = new DummyObject();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // assigning value
        dummyObject.Name = "I am Dummy";

        // Passing object
        Form2 frm = new Form2(ref dummyObject);
        frm.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // Displaying Name
        MessageBox.Show(this.dummyObject.Name);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        // Assigning new object
        this.dummyObject = new DummyObject();

        // Changing Name Variable
        this.dummyObject.Name = "I am Rechanged";

        // Displaying Name
        MessageBox.Show(this.dummyObject.Name);
    }
}

Class to which Object is passed by Reference:

public partial class Form2 : Form
{
    private DummyObject dummyObject = null;

    public Form2(ref DummyObject DummyObject)
    {
        InitializeComponent();

        this.dummyObject = DummyObject;
        this.dummyObject.Name = "I am Changed";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.dummyObject.Name);
    }
}

whn i reaasign the object in Form 1 and cdisplay its value in form 2 it still displays “I am Changed” instead of “I am Rechanged”.

How to keep the data synchronized?

  • 1 1 Answer
  • 1 View
  • 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-14T14:09:22+00:00Added an answer on May 14, 2026 at 2:09 pm

    You cannot keep variables synchronized in this manner; there is no concept of ref instance or static variables, only ref parameters. The dummyObject instance variable does (and always will) represent a distinct slot of memory. All you’re doing is copying the value from the DummyObject parameter into dummyObject; you’re doing nothing that would be affected by whether or not the parameter is being declared as ref.

    The typical way is to expose the value of dummyObject as a property on Form2.

    public DummyObject DummyObject
    {
        get { return dummyObject; }
        set 
        { 
            dummyObject = value;
    
            // any other code, if any, that might need to execute 
            // when the value is changed
        }
    }
    

    But this means that you’ll need to hold on to your instance of Form2 so that you can change the value of the property.

    Another option, though somewhat convoluted, would be pass a wrapper class that contains that property, rather than adding it to the form.

    public class DummyWrapper
    {
        public DummyObject DummyObject { get; set; }
    }
    

    You then change your forms to use a DummyWrapper instead of a DummyObject, then access the dummyWrapper.DummyObject property when you want to get or set the value. Uas long as you only change the value of the DummyWrapper.DummyObject property and not the actual value of the DummyWrapper, then you’ll be pointing at the same instance.

    For instance:

    public partial class Form2 : Form 
    { 
        private DummyWrapper dummyWrapper = null; 
    
        public Form2(DummyWrapper dummyWrapper) 
        { 
            InitializeComponent(); 
    
            this.dummyWrapper = dummyWrapper;
            this.dummyWrapper.DummyObject.Name = "I am Changed"; 
        } 
    
        private void button2_Click(object sender, EventArgs e) 
        { 
            MessageBox.Show(this.dummyWrapper.DummyObject.Name); 
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.