Shashi,
Thanks for the code. I changed it to reflect what I´m saying.
The second print should be "Created by makeChanges()", because the object was passed as a reference in constructor. It should assign the new instance (it occurs if the new instance is created in the constructor).
Seens C# lost the reference when it goes outside the method with ref, even if you assign the reference to a variable....
User Profile
Collapse
-
Shashi,
I was creating a new instance of myObj in the method. If I create in the constructor, it will reflect OK in main class. Creating in method, it doesn´t reflect in main class, even using ref in the constructor.
I think it should, because myObj is actually a pointer to the reference of the object (using ref). New instance should be visible when returning from method....Leave a comment:
-
C# ref in constructor and change in method
Hello all!
I´m passing a reference using ref to a constructor in class A.
A a = new A(ref myObj);
If I change values of myObj in class A, they are reflected on the main class.
But, If I call a method like:
a.myMethod()
And change values of myObj (a private variable inside class A, assigned from ref by the constructor), they are not reflected on the main class.
Any work around?...
No activity results to display
Show More
Leave a comment: