Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell HashTable - self referencing during initialization

I have a theoretical problem - how to reference a hash table during its initialization, for example, to compute a member based other already stated members.

Remove-Variable myHashTable -ErrorAction Ignore
$myHashTable = 
@{
    One = 1
    Two= 2
    Three = ??? # following expressions do not work 
        # $This.One + $This.Two or 
        # $_.One + $_.Two
        # $myHashTable.One + $myHashTable.Two
        # ????
}

$myHashTable.Three -eq 3 # make this $true

Any ideas how to do it? Is it actually possible?

Edit: This was my solution:

$myHashTable = 
@{
    One = 1
    Two= 2
}
$myHashTable.Three = $myHashTable.One + $myHashTable.Two
like image 302
honzajscz Avatar asked Jul 31 '16 15:07

honzajscz


1 Answers

This won't be possible using the object initializer syntax I'm afraid. While it is possible to use variables, you'll have to compute the values before creating the object.

like image 175
Lunster Avatar answered Sep 19 '22 01:09

Lunster