Search code examples
c#vectorxnareturnpass-by-value

Cannot Modify Return Value because It is not a Variable


Pass by Value strikes again!!

player.Vector.X += player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

does not work.

Vector2 v = player.Vector;
v.X -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
player.Vector = v;

fixes this problem.

This is explained here: Can't modify XNA Vector components

The answer was very well explained and works just fine, but it has been 4 years since it was posted. My question is, since its been 4 years, is there a better way to fix this problem now?

I have about 100 lines of this I need to fix, and was hoping there was some sort of shortcut by now.


Solution

  • There is probably never going to be a "shortcut" for this since it's a feature of the fundamental difference between value types and reference types.