Change values of an array of pscustomobject's with a for-loop

1.1k views Asked by At

I have tried to create a array of custom objects (pscustomobject) and now i tried to change some values of the different custom objects with an for-loop. But it doesn't seem to work. Here is what i tried:

$obj = @([pscustomobject]@{value=0;type="D";used=$false})
$arr1 = @($obj) * 10

for($v = 0; $v -lt 4; $v++){
 $arr1[$v].value = ($v+1)
 $arr1[$v].type ="bubble"
}

The result is:

                        value type                                                   used
                    ----- ----                                                   ----
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False
                        4 bubble                                                False

But i expected that the result will be:

                        value type                                                   used
                    ----- ----                                                   ----
                        1 bubble                                                False
                        2 bubble                                                False
                        3 bubble                                                False
                        4 bubble                                                False
                        4 D                                                     False
                        4 D                                                     False
                        4 D                                                     False
                        4 D                                                     False
                        4 D                                                     False
                        4 D                                                     False

This is only a snipped, and i was only trying something out. But i am a bit annoyed that i don't get it... Sorry i think it's a easy think but don't see whats worong.... i am still a PS noob... :-/

EDIT: PSv3 is used, but i think it doesn't matter...

1

There are 1 answers

0
mjolinor On BEST ANSWER

This:

$arr1 = @($obj) * 10

is not creating 10 new objects. It created 10 references to the same object.

Note the difference if you do this:

$arr1 = 1..10 |% {[pscustomobject]@{value=0;type="D";used=$false}}

for($v = 0; $v -lt 4; $v++){
 $arr1[$v].value = ($v+1)
 $arr1[$v].type ="bubble"
}
$arr1

                                       value type                                                                                 used
                                       ----- ----                                                                                 ----
                                           1 bubble                                                                              False
                                           2 bubble                                                                              False
                                           3 bubble                                                                              False
                                           4 bubble                                                                              False
                                           0 D                                                                                   False
                                           0 D                                                                                   False
                                           0 D                                                                                   False
                                           0 D                                                                                   False
                                           0 D                                                                                   False
                                           0 D                                                                                   False