If I had an object of which I only needed access to a particular part of, should I pass in just that particular part or should I pass the entire object in?
In my case, I’m building a game with C# using XNA and I need access to the “bounds” of the Camera class in “DrawLayer.cs”, a class that draws a single layer (assuming there’s multiple layers due to parallax) of the game onto the screen. The “bounds” of the camera exist as a property below:
public Rectangle Bounds { get { return onScreenCamera.Bounds; } }
The game is a multiplayer game that has a camera that jumps between players depending on their actions, so “onScreenCamera” references the camera that currently needs to be used. Also, the camera functions differently depending which player type you are, so I made an interface ICameraSetup.cs that the camera I’m discussing implements. Here’s the ICameraSetup interface:
interface ICameraSetup
{
Vector2 Position { get; }
Rectangle Bounds { get; }
void Update(GameTime pGameTime);
}
With DrawLayer.cs, I wanted to write the constructor header like this (note that Rectangle is a struct, which should mean it’s a value-type):
public DrawLayer(ref Rectangle pBounds)
and then with the constructor call, it was like this:
new DrawLayer(ref levelState.Camera.Bounds)
This would be passing only the bounds of the camera into DrawLayer.cs, but I seem to be getting this error: “The Property Game.Camera.ICameraSetup.Bounds has no setter.” The issue is, I don’t want the interface to declare a setter for Bounds because the only class that should be able to set bounds is the Camera class and the way Bounds is set is through the private variable in the camera class, mentioned above, called “onScreenCamera”.
But, even if I add Set to the Bounds property, I still get this error at the DrawLayer.cs constructor call: “‘ref’ argument is not classified as a variable.”
Now, one solution that I know works is to pass the entire camera into DrawLayer.cs and use bounds from there, like this:
public DrawLayer(ICameraSetup pCamera)
then
new DrawLayer(levelState.Camera)
then use Camera.Bounds, but this seems unnecessary as all I need are the bounds in that class.
Another issue I might be having is with my understanding of pass-by-reference. I understood it to be useful when you need a value-type reference to stay updated with the whatever changes are made to the value outside of the class you passed it into, but I’ve also read that it was the other way around: use it to change a value-type outside of where it’s initially declared and have it stay the same.
So am I making a mistake with my passing-by-reference of the Bounds of the Camera into DrawLayer?
You’re ignoring the first error:
Additionally, passing an argument by reference in this way means that it can be set, which it cannot because you define no public setter for the
Boundsproperty. So, we have two problems.I’m confused; why are you attempting to pass it by reference to begin with? You wish to store a reference to this object so that it can be changed later (FYI: you can’t)? Sounds like a poor design to me to be honest. This doesn’t seem like a performance concern, so I would suggest tweaking your design.
When you run into things like this which are weird and become clunky/don’t work well, it usually means you have a design problem on your hands. Don’t attempt to fix that by introducing more design problems.