Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL.NET Primitives / C# Properties

Started by pokoto Jul 14, 2008 at 10:07 PM 0 replies 1.2k views
Original Post
pokoto
pokoto
I am making a Pong clone to get familiar with Primitves and SDL.NET in general. My paddles are SdlDotNet.Graphics.Primitives.Box objects, and I am trying to get them to move using this code: paddle.Location.Y -= 2; This would be done whenever the up arrow is hit. It gives an error on compile, saying "Cannot modify the return value of 'SdlDotNet.Graphics.Primitives.Box.Location' because it is not a variable." So I tried this code to try to figure out what was going wrong (Location is a Point property, by the way): Point testPoint = new Point(0,0); testPoint.Y -= 2; It compiled fine. Why doesn't the first code work?
vermilion_wizard
vermilion_wizard
Because Paddle.Location returns a Point, which is a value type (struct). When structs are returned, they are copied, so when you do Paddle.Location.Y you are accessing the "Y" property of the temporary copy of the struct, something which is virtually always an error so the compiler doesn't allow it.

Either do:
paddle.Location = new Point(paddle.Location.X, paddle.Location.Y - 2);

or look for (or add) an X, Y property on the paddle object that modify the field backing the Location property.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.