0

I have an interface class Position with get and set methods.

Then I have classes Objects1,Objects2,... that extends interface Position. And in the end I have class Scene which have List with getter and setter for this list. I am saving my Objects1, Objects2 in it.

Then I have a class Level that extends class Scene. There I created Objects1,Objects2,... and add them to the List.

When I get the first item from the List and I want to call that set function to set Position it says “Cannot modify the return value because it is not a variable”. How to solve this?

interface Position
    {
        public Vector2 PositionCords
        {
            get; set;
        }
    }
class Scene
    {
        private List<Position> _Items;
        public Scene() 
        {
            _Items = new List<Position>();
        }

        public List<Position> SceneItems
        {
            get => _Items;
            set => _Items = value;
        }
    }
class Objects1 : Position
    {
        public Vector2 PositionCords
        {
            get;set; 
        }
    }

    class Level: Scene
    {
        private Scene _scene;
        private Objects1 _itemA;

        public Level()
        {
            _scene = new Scene();
            _itemA = new Objects1();
            _scene.SceneItems.Add(_itemA);
            _scene.SceneItems.Add(_itemA);

            //error
            _scene.SceneItems[0].PositionCords.X = 0;

        }
    }
zed
  • 3
  • 4
  • This happens because `Vector2` is a struct, and `...PositionCords.X = 0` does not do what you'd expect it to do. I have linked your question to a duplication question that explains why this happens (and what to do about it) in more detail. – Heinzi Nov 18 '22 at 12:18
  • `Vector2` is a `struct`, so the methods return a copy from it and a property is like a method. So you cannot modify the struct directly, but you can re-assign/re-create it: `var firstScene = _scene.SceneItems.First(); firstScene.PositionCords = new Vector2(0, firstScene.PositionCords.Y);` – Tim Schmelter Nov 18 '22 at 12:22

0 Answers0