Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Instantiate Objects Automatically or with Iteration

    class Molecule{
    [Int]$carbon
    [Int]$hydrogen
    [Int]$oxygen
}

$items = [ordered]@{
    water = [ordered]@{carbon = 0; hydrogen = 2; oxygen = 1}
    methane = [ordered]@{carbon = 1; hydrogen = 4; oxygen = 0}
    }
 
$EachKey1 = @() # Just for T-Shooting
$EachObj = @() # Just for T-Shooting
    
foreach ($key in $items.psbase.keys){
     $EachKey1 += $key
    $key = New-Object Molecule
     $EachObj += $key
    $key.carbon = $items.$key.carbon
    $key.hydrogen = $items.$key.hydrogen
    $key.oxygen = $items.$key.oxygen
}

""
$EachKey1 # Anticipated Values
$EachObj # Not getting values

######################### Output Below #########################

Script Output

Please assist with Instantiating Objects Automatically or via Iteration.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am unable to instantiate Objects Water & Methane as attempted above.

Thanks!

>Solution :

It’s not entirely clear what your expected output would be, however, the clear mistake is in this line:

$key = New-Object Molecule

The item you’re iterating over (the hash table Key) is getting replace by a new instance of the Molecule object, the when doing the Property assignment:

$key.carbon   = $items.$key.carbon
$key.hydrogen = $items.$key.hydrogen
$key.oxygen   = $items.$key.oxygen

You’re effectively assigning $null to each Property Value, however, since your class properties are constrained to the type [int] PowerShell is converting $null to 0:

[int] $null # => 0

If you want an array of Molecule objects you can simply do it this way:

$EachObj = foreach ($key in $items.PSBase.Keys) {
    [Molecule] $items[$key]
}

PowerShell converts the ordered dictionary into an instance of the class by type casting or type conversion. The result would be the following:

PS /> $EachObj

carbon hydrogen oxygen
------ -------- ------
     0        2      1
     1        4      0

PS /> $EachObj[0] | Get-Member

   TypeName: Molecule

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
ToString    Method     string ToString()
carbon      Property   int carbon {get;set;}
hydrogen    Property   int hydrogen {get;set;}
oxygen      Property   int oxygen {get;set;}

Type casting into an array of Molecule instances would also work:

PS /> $EachObj = [Molecule[]] $items[$items.PSBase.Keys]

PS /> $EachObj.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Molecule[]                               System.Array
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading