Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

xna

I'm wondering how you'd recommend designing 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 Vector2 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?

like image 553
Nate Avatar asked Dec 30 '25 19:12

Nate


1 Answers

Or, just to throw this out there ... just expose the field. There's nothing inherently wrong with exposing a public field.

For example, if your entity exposes a Vector3 for it's position so that other things can use that value in their own calculations ... just expose it. Otherwise, if no other class or entity needs to know the position, do not expose it at all :-)

Here is some sage advice from Rico Mariani:

Generally, my feeling is that properties are highly overrated and fields terribly under-utilized. Did I mention that not everyone agrees with this position? :)

like image 64
Joel Martinez Avatar answered Jan 01 '26 07:01

Joel Martinez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!