Reference to an object is not updating the object

前端 未结 3 1745
旧巷少年郎
旧巷少年郎 2021-01-29 14:38

Hi I am working with code that looks something like this .

class A
{
    Custom objA;

    public A()
    {
        //Assign some value to objA;
        B obj =          


        
相关标签:
3条回答
  • 2021-01-29 15:20

    It seems to me that this is the code that you're asking about in your question:

    class A
    {
        Custom objCustom;
    
        public A()
        {
            objCustom = new Custom();
            B objB = new B(objCustom);
            objB.Func();
        }
    }
    
    class B
    {
        Custom objCustom;
    
        public B(Custom objCustom)
        {
            this.objCustom = objCustom;
        }
    
        public void Func()
        {
            this.objCustom = null;
        }
    }
    

    This compiles and has a fairly consistent naming convention.

    The issue you have the with the Custom objCustom field in both class A and B is that even though they both reference the same instance before the call to .Func() you need to understand that the Custom objCustom field is just a reference to the object, and not the object itself. So when you call .Func() you are assigning a new reference to the Custom objCustom in objB, but you're not doing anything to the reference in objA. It still points to the original object.

    0 讨论(0)
  • 2021-01-29 15:20

    In function Func you assign null to the field of B object (not A object) so after this function call field objB of B object is not pointing to any object of type Custom, but objA field of A object is still pointing to a single Custom object.

    Here is example of code, where Func will change objA field of A object:

    class A
    {
        public Custom objA = new Custom();
    
        public A()
        {
            B obj = new B(this);
            B.Func(); // after this, objA field will be null
        }
    }
    
    class B
    {
        А obj;
    
        public B(А obj)
        {
            this.obj = obj;
        }
    
        public void Func()
        {
            obj.objA = null;
        }
    }
    

    Also note:

    1. When you create an A object, its objA field is null, so in constructor of A you actually calling B obj = new B(null);, so its objB field is null too.
    2. After A object is constructed, object obj of type B will be disposed, since you don't hold a reference to it, so you not calling a Func function in your example.
    0 讨论(0)
  • 2021-01-29 15:23

    Firstly, on line 5, you have a syntax error. You could do B obj = new B(objA) if you are trying to create a new instance of B. But I can only guess.

    What it seems like you are trying to do is modify objA by having passed it into a new object of type B, storing it in a field, and then modifying the field. The problem is that what you are storing is a copy of a reference to objA. When you do this.objB = null, you are modifying the field objB to have a new reference (null), but you have not done anything to the field objA which is a member of the instance of class A.

    0 讨论(0)
提交回复
热议问题