Properties: a.b.c = 3

Nick Sabalausky a at a.a
Thu Jul 30 12:32:58 PDT 2009


"Ary Borenszweig" <ary at esperanto.org.ar> wrote in message 
news:h4pn90$3070$1 at digitalmars.com...
>
> Yes they can. And also C# shows us the solution to the problem (similar to 
> what Walter proposed).
>
> ---
> public class Bar
> {
>     public Foo Foo { get; set; }
> }
>
> public struct Foo
> {
>     public int Property { get; set; }
> }
>
> Bar bar = new Bar();
> Foo foo = new Foo();
> foo.Property = 10;
> bar.Foo = foo;
>
> bar.Foo.Property = 20; // line 16
> ---
>
> Error on line 16: Cannot modify the return value of 'Bar.Foo' because it 
> is not a variable

C# shows us *A* solution. Doesn't mean we can't do one better.

The whole point of properties is that, to the outside observer, they behave, 
as much as possible, like plain fields. Obviously this can't be done in all 
cases (like getting an int* from &myIntProp), but in this case:

widget.sizeAsAStructField.width = 10; // Works
widget.sizeAsAStructProp.width = 10; // Doesn't work

...we absolutely can fix that even if C# doesn't.





More information about the Digitalmars-d mailing list