Accessing outer class attribute from inner struct

Andre Pany via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 28 15:47:12 PDT 2017


On Monday, 28 August 2017 at 22:28:18 UTC, Moritz Maxeiner wrote:
> On Monday, 28 August 2017 at 21:52:58 UTC, Andre Pany wrote:
>> [...]
>>
>> To make my question short:) If ColumnsArray is a class I can 
>> access the attribute "reference" but not if it is a struct. I 
>> would rather prefer a struct, but with a struct
>> it seems I cannot access "reference".
>>
>> How can I access "reference" from my inner struct?
>>
>> [...]
>
> Add an explicit class reference member to to it:
> ---
> class TCustomGrid: TCustomPresentedScrollBox
> {
> 	struct ColumnsArray
> 	{
> 		TCustomGrid parent;
>
> 		TColumn opIndex(int index)
> 		{
> 			int r = getIntegerIndexedPropertyReference(reference, 
> "Columns", index);
> 			return new TColumn(r);
> 		}
> 	}
> 	
> 	ColumnsArray Columns;
>
> 	this()
> 	{
> 		Columns = ColumnsArray(this);
> 	}
> ...
> }
> ---
>
> Nesting structs inside anything other than functions[1] is for 
> visibility/protection encapsulation and namespacing only.
>
> [1] non-static structs in functions are special as they have 
> access to the surrounding stack frame

Unfortunately thats not possible. ColumnsArray and the attribute 
will become a string mixin to avoid boilerplate.

It would be error prone if I have to initialize them in the 
constructor too. I want just 1 single coding line for this 
property. That is also the reason I do not want to use a class, 
as I would have to initialize them in the constructor.

Kind regards
André


More information about the Digitalmars-d-learn mailing list