Cags
Contributor
I recently ran into a problem in my application and it took me awhile to track down the cause. The code below demonstrates the problem. Basically the problem revolves around calling a Method of a Property of a class. Insead of doing as I expected it to, it just didn't do anything. This leads me to believe you cannot call a Method of a Property, but if this is the case I would have expected an error at either compile time or possibly at runtime. I'm quite resigned to the fact this is the way it is and I will probably have to live with it, I was just looking for an explanation as to why this is the case.
C#:
private class Piece
{
private Rectangle _bounds;
public Rectangle Bounds
{
get { return _bounds; }
set { _bounds = value; }
}
public Piece()
{
_bounds = new Rectangle(10, 10, 100, 100);
}
}
private void Test()
{
Piece myPiece = new Piece();
System.Diagnostics.Debug.WriteLine("Location = " + myPiece.Bounds.X.ToString() + ", " + myPiece.Bounds.Y.ToString());
myPiece.Bounds.Offset(displacement);
System.Diagnostics.Debug.WriteLine("Location = " + myPiece.Bounds.X.ToString() + ", " + myPiece.Bounds.Y.ToString());
myPiece.Bounds = new Rectangle(myPiece.Bounds.X + displacement.X, myPiece.Bounds.Y + displacement.Y, myPiece.Bounds.Width, myPiece.Bounds.Height);
System.Diagnostics.Debug.WriteLine("Location = " + myPiece.Bounds.X.ToString() + ", " + myPiece.Bounds.Y.ToString());
}
// This is the output from the console
// Location = 10, 10
// Location = 10, 10
// Location = 12, 12