Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Referencing a parent object from a nested child object

$
0
0

Consider:

$Parent = New-Object PSCustomObject
$Parent|Add-Member -MemberType NoteProperty -Name ThisObj -Value {$this}

$Child = New-Object PSCustomObject
$Child|Add-Member -MemberType NoteProperty -Name ThisObj -Value {$this}
$Child|Add-Member -MemberType ScriptMethod -Name CheckThisObj -Value {$this.thisobj}

$Parent|Add-Member -MemberType NoteProperty -Name ChildObj -Value $Child

That's all very well, and is easy to navigate.  If I want to see the value of 'Child', I can call $Parent.ChildObj.<property|method>

But how can I refer to the parent from the child?  Essentially, I want to be able to do something like:

$Child|Add-Member -MemberType NoteProperty -Name MyParent -Value {$this.parent.ThisObj}

To return a parent object property.  Whilst I could quite easily be explicit in referring to the parent:

$Child|Add-Member -MemberType NoteProperty -Name MyParent -Value {$Parent}

$Child.MyParent.thisObj

It doesn't help when I'm nesting the 'Child' in several parents, and I want to find a property of the parent to determine the child behaviour.

So:

$Parent1|Add-Member -MemberType NoteProperty -Name Child -Value {$Child}

$Parent2|Add-Member -MemberType NoteProperty -Name Child -Value {$Child}

$Parent2.Child.Property = "NewValue"

$Parent2.SaveMethod()

Essentially saves the value set in the ChildProperty in the parent.

Is this possible?


Viewing all articles
Browse latest Browse all 15028

Trending Articles