PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] Why does reinstantiating an object delete any references other objects may have had to it?

 July 09, 2022     c#, generics, reference     No comments   

Issue

I made a generic class that holds a simple object (PassedObject) which just contains an integer. To understand where my container loses its reference to my initial 'PassedObject' instance, i printed the data after simply changing the number field and again after making a new instance of b.

PassedObject b = new PassedObject(20);
ObjectHolder<PassedObject> objectHolder = new ObjectHolder<PassedObject>(b);

objectHolder.PrintData(); // prints 20
b.number = 888;
objectHolder.PrintData(); // prints 888
b = new PassedObject(7782);
objectHolder.PrintData(); // prints 888
b.number = 2;
objectHolder.PrintData(); // still 888, lost reference...

public class PassedObject
{
    public int number { get; set; }

    public PassedObject(int number)
    {
        this.number = number;
    }
}

public class ObjectHolder<T> where T : PassedObject
{
    public T passedObject;

    public ObjectHolder(T newObject)
    {
        passedObject = newObject;
    }

    public void PrintData()
    {
        Console.WriteLine(passedObject.number);
    }
}

My question is: Given that my objectHolder still points to the same variable (b), why do I lose that reference to b after reconstructing it?


Solution

The code in the question has two PassedObject references: one in the b variable, and one in the objectHolder.PassedObject variable.

Initially, the code creates a new PassedObject instance and assigns it to the b reference. Then the b reference is used to also set the objectHolder.PassedObject.

At this point, both references refer to the same object instance, but they are still two different variables. Because they refer to the same object, we can update properties via one reference and we will see the change in the other. This is why the change to 888 works.

However, updating a property is not the same as replacing the reference itself. The next thing the code does is create brand new PassedObject instance and assign it to the b reference. This assignment does not also update objectHolder.PassedObject.

As this point the two references no longer refer to the same object. The objectHolder.PassedObject variable continues to refer to the prior 888 instance. Since it is a completely different reference, updating the .number property via the b reference won't change anything.



Answered By - Joel Coehoorn
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing