XNA Class Design with Structs as Properties and issues because they are value types and not referenc

Posted by Nate Bross on Stack Overflow See other posts from Stack Overflow or by Nate Bross
Published on 2010-03-23T03:26:40Z Indexed on 2010/03/23 16:03 UTC
Read the original article Hit count: 258

Filed under:
|
|
|
|

I'm wondering how you'd recommend designin a class, given the fact that XNA Framework uses Struct all over the place?

For example, a spite class, which may require a Vector2 and a Rectangle (both defined as Struct) to be accessed outside of the class.

The issue come in when you try to write code like this:

class Item 
{
    public Vetor2 Position {get; set;}
    public Item() { Position = new Vector2(5,5); }
}

Item i = new Item();
i.Positon.X = 20; // fails with error 'Cannot modify the return value of Item because it is not a variable.'

// you must write code like this
var pos = i.Position;
pos.X++;
i.Position = pos;

The second option compiles and works, but it is just butt ugly. Is there a better way?

© Stack Overflow or respective owner

Related posts about c#

Related posts about design