C# / XNA: Passing Class instances (objects)

So, not being an expert I was trying to find improved ways of managing external class member access  without resorting to “public static” and ideally wanted an object orientated method, a while ago we were told by Rob Miles how classes or specifically instances of classes (objects) are passed by reference. I understood this however I didn’t really join the dots together for one useful implication it has::

Passing objects as parameters allows the receiving Method and potentially it’s Class, access to the objects members directly. (since its a memory reference to that object, not a copy).

It maybe obvious to some but ultimately,  it means you don’t need to keep feeding objects in as parameters for update methods to show changes, you can just do it once and store it there as a class member. The objects members will all be directly accessible from within that class, even if they are changed elsewhere in your program/game.

I think the thing that didn’t make this obvious to me is the fact that if you pass a class instance member variable instead of the instance itself, this behaviour doesn’t work, you just end up with a copy of the member variable at the point of being passed, but that’s because it’s been passed by value, not reference. Additionally, I found myself repeatedly passing object instances into Update methods, which worked but simply wasn’t needed since a ref could just be passed once and stored for the class. In the context of “Sweepy Cleaner” our Uni course work project, the below highly contrived examples highlight what I mean:

Pass by value Example:

Pass by Reference Example:

The other obvious good thing about passing the class instances instead of specific member variables is that you can of course access all members of that class instead of just the ones you pass. Additionally, it gets around the need to use public static for easy but rather lazy access of class members from outside the class, because only the classes you pass the instance to will be able to access it’s public members, and if you combine with “accessor”/”getter” methods I guess they don’t even need to be public.

(I’ve heard bad things about getter/setters too because it goes against the object orientated nature of C# apparently, but I’m not too bothered about that at this stage).

Anyway, you can of course still pass individual members via the “ref” keyword in the parameter and arguments to achieve the same thing as above but just for a member. I personally think for XNA passing the instance just seems a sound way of doing things. I’m not sure if there are repercussions to doing it like that, but so far it seems sound and hope this may help others who also hadn’t realised its usefulness.

Advertisements

3 thoughts on “C# / XNA: Passing Class instances (objects)

  1. It’s really good to see students really thinking about what they are doing, although there are a few things I should point out.

    The real power with using objects is the ability to create instances of objects. When you talk about using public static to access data that’s not quite the same thing, because the static means that it belongs to the class as a whole, rather than the individual instance of the class. For example, if you had two hoover instances but their hooverDead property or member was static, then when one dies the other one would die too, because the static data is shared across all instances.

    As far as I’m aware there is nothing bad about getters and setters, in fact properties are first class members of C# and you should use them over the old style accessor and mutator methods because the can be used to provide automatic functionality in all sorts of data context binding scenarios.

    The other big advantage about passing objects by reference is that if you pass something using it’s reference then the reference is just 4 bytes (or 8 bytes if you are using x64 OS). Quite often classes contain a lot more data than that. Passing by reference is faster, and uses less memory. You also have to call a constructor to pass a class by value, so there is an overhead there as well.

    From my perspective (coming at C# from a C++ background) one disadvantage of passing by reference is precisely that you give anything you pass an object to access to all it’s public data. In C++ this is addressed by the fact that you can mark class functions as const, meaning that the function is unable to change any of that objects data. This means you can then pass objects to other objects as const references, meaning that they can’t change any of that objects data either.

    You don’t need to worry about any of that until next year though.

    Sorry if any of that seems a little preachy. The overriding thing I think is that it really is good that you are considering all this stuff and reflecting on what you are learning. Rather than focussing on making it work you’re making an effort to make it work in a good way, and understanding how you made it work too 😀

    • Thanks for the response, not preachy at all 😀 It’s interesting to hear the pro’s and con’s, I’m also looking forward to doing stuff with C++. I find in programming there’s so many ways to do something, but often on the net there’s conflicting or subjective views on what the best methods are.

      Like you said there’s definitely scenarios when static members wouldn’t work, I think why I mentioned public static members is because for occasions where something doesn’t need to be tied to an instance it seems to allow you to access it instantly from any where without hassle, because you don’t need to worry about having passed an instance of it’s class first. For example if you created a static class called Global and put lot’s of public static members in it for various things that you want to access like gameScore, gamestates and bools etc and then accessed e.g Global.gameScore from anywhere you need to change the score. I understand that to be “lazy” programming, and static used like that bad because it kind of sets your code in stone and isn’t the object orientated way of doing things…also I suppose it’d be too accessible?

      Public static certainly came in handy for Three Thing Game though when programming aesthetics tended to go out of the window in favour of speed 😛

      • Yeah, anything global is generally bad, but sometimes it’s not worth the effort of avoiding the bad things. If you must do that then you could make private static data and public accessors to control how the class is accessed. Rather than taking someone’s word for it you should decide for yourself what is bad and what isn’t. If you understand why something is bad then you can avoid those pitfalls and use the good that is left, if any. There are always pros and cons to how things are done. The best way to do anything usually comes down to what the meaning of “best” is. In programming, is best fastest, most secure, smallest memory footprint, most readable, most reusable, fewest bugs. These are almost always conflicting. One tactic when you begin a project is to pick a few principles and stick to them. For example, in three thing game you’re unlikely to approach the limits of the hardware, so you can probably get away with ignoring how fast your code runs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s