Tutorial :Having an issue with the “this” modifier



Question:

I have this method in City class. It should create a new city based on the object which the method is applied to:

 public City newCity(string newCityName, int dX, int dY)      {          City c=new City(this); //based on a constructor : City(City c){}            c.CityName=newCityName;          c.NoOfNeighborhoods=1;          c.NumOfResidents=0;          c.CityCenter.Move(dX,dY);            return c;      }  

CityCenter is of type "Point" which has two fields - x,y. the Move method in Point class is ment to change the CityCenter location. It looks like this:

 public void Move(int dX, int dY)      {          this.X = x + dX;          this.Y = y + dY;      }  

What happens is that the new object,c and the existing City object are both changed. I think that "this" modifier works on the existing object too...

How can I take advantage of the Move method without causing this behavior? Note: this is a closed API, so I can only add private methods to the project.


Solution:1

My guess is that Point is a class, so you are sharing the reference to the same instance of the point. You will need to create a new instance of the Point and assign that to the new City.CityCenter


Solution:2

I suspect City c=new City(this); is creating a shallow clone of the current City which means they both share the same Point object (could only be true if Point is a class and not a struct).

Can you do City c=new City(); instead?


Solution:3

The problem is (almost certainly) that both cities have a reference to the same Point object. When you change the object, that change is seen through both references. Options:

  • Create a new Point object when you clone the city
  • Make Point a value type (so that an independent copy is made
  • Make Point an immutable type and change Move to return a new Point with the relevant change made

(Or some combination of the above...)

It sounds to me like Point should probably be a value type (a struct). Note that structs should almost always be immutable.

It seems somewhat odd to have a newCity instance method in the first place - what relation is the new city meant to have to the old city? Why aren't you just creating a completely separate city?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »