D
Dave
..X (.Y too) are ints with get/set methods so why does trying to set either
one generate a compile error??
one generate a compile error??
Rene said:Not really, you can actually do what are doing as long as you had a
direct reference to the Point variable:
Rene said:Not really, you can actually do what are doing as long as you had a direct
reference to the Point variable:
-----------------------------------
public class Test
{
public Point _Point = new Point();
}
Test myTest = new Test();
myTest._Point.X = 0; // This will work
-----------------------------------
This is not about value type, this is about the fact that internally you are
getting and setting a *Full* Point struct, so assigning just a member of a
struct does not work.
... or if Point was a reference type, in which case the property getter
doesn't return a copy of the variable, but rather a new reference to it.
Jon Skeet said:Carl Daniel [VC++ MVP]
... or if Point was a reference type, in which case the property getter
doesn't return a copy of the variable, but rather a new reference to it.
No, it would return a copy of the variable's value. The variable isn't
the same as its value - what would be returned would have nothing to do
with the variable itself, it would just have the same value.
Specifically, there is nothing you could do with the returned value
which would change the value of the original variable. You could change
the contents of the object that value referred to, but that's not
changing the variable's value.
Nick Hounsome said:I think that we all know what he means.
The lesson to be learned from this is that value types with setable
properties are a bad idea i.e. changing value types should be an all or
nothing affair.
Agreed.
Note there are at least as many problems caused by using reference types.
The usual scenario is that you have a class with something like a
LocationChangedEvent which is only raised on "obj.Location = newLocation;"
and not "obj.Location.X = 42;"
The real solution is C++ const but it isn't going to happen![]()
it would be equivalent to:
Point p = this.Location;
p.X=0;
Rene said:I am not sure if I agree with that because if I follow the same logic, if I
try to set the "this.Top = 0" it would be equivalent to:
int i = this.Top;
i = 0;
This would also be a no-op (I assume this is stands for no-operation) so the
compiler should not allow me to do that either but it does.
Going back to the Location property again, In my opinion, the compiler will
internally try to use the set_Location(Point newPoint) function to set the
new value for the Location property. As we all already know, this function
is automatically created by the compiler.
this.Location = said:The only thing that makes sense to pass as the argument to this function is
a full Point variable and not just a member like "someNewPoint.X".
You just can't partially change a Point value like that; it would be like
trying to change an integer value of zero to a one by trying to pass only
the one bit that it needs to change form a 0 to a 1. You don't do that,
what you do is to pass the whole integer value as in "00000000 00000000
00000000 00000001".
I believe this is really why the compiler won't allow the code to compile.