Modifying a property to automatically resolve "Cannot modify the return
value" error
I'd like to know if there's a built-in or an easy to implement way to
handle this "Cannot modify the return value because it is not a variable"
problem.
Let's say I have this class:
MyClass
{
Rectangle _rect = new Rectangle(1, 2, 3, 4);
public Rectangle rect { get { return _rect; } set { _rect = value; } }
}
This is what I'm trying to do:
rect.Width += 20; // and this is where the error pops up
The usual approach would be to do this instead:
rect = new Rectangle(rect.X, rect.Y, rect.Width + 20, rect.Height);
But there has to be a way to automate it, right? And I don't mean just
adding a bunch of properties to MyClass like rect_width and such (because
I have 9 rectangles in my real class implementation, and that would be
just bad), but something that would make this line of code work:
rect.Width += 20;
No comments:
Post a Comment