Reference to an object is not updating the object

前端 未结 3 1784
旧巷少年郎
旧巷少年郎 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: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.

提交回复
热议问题