Properties: a.b.c = 3
Ary Borenszweig
ary at esperanto.org.ar
Thu Jul 30 13:00:57 PDT 2009
Nick Sabalausky wrote:
> "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.
I don't understand your example. What's wrong with it? What doesn't work
as you expect and what is your expected result?
In the first case, if you modify sizeAsAStructField.width, it's ok and
should compile, because you are modifying sizeAsAStructField itself, not
a copy of it. In C#:
public struct Foo
{
public int Property { get; set; }
}
public class Bar
{
public Foo Foo;
}
static Main()
{
Bar bar = new Bar();
Foo foo = new Foo();
foo.Property = 10;
bar.Foo = foo;
bar.Foo.Property = 20;
// prints 20, as expected
Console.WriteLine(bar.Foo.Property);
}
More information about the Digitalmars-d
mailing list