c# - Modifying a property to automatically resolve "Cannot modify the return value" error -
i'd know if there's built-in or easy implement way handle "cannot modify return value because not variable" problem.
let's have class:
myclass { rectangle _rect = new rectangle(1, 2, 3, 4); public rectangle rect { { return _rect; } set { _rect = value; } } }
this i'm trying do:
rect.width += 20; // , error pops
the usual approach instead:
rect = new rectangle(rect.x, rect.y, rect.width + 20, rect.height);
but there has way automate it, right? , don't mean adding bunch of properties myclass
rect_width
, such (because have 9 rectangles in real class implementation, , bad), make line of code work:
rect.width += 20;
there's nothing can circumvent problem, since rectangle-class immutable , that's way intended work.
however, expose method encapsulates creation of new rectangles:
myclass { rectangle _rect; public rectangle rect { { return _rect; } set { _rect = value; } } public void addwidth(int width) { rect = new rectangle(rect.x, rect.y, rect.width + width, rect.height); } }
Comments
Post a Comment