I have encounter some strange issue that I cannot solve so hopefully someone can shed some light in this. Not sure why, but anything I do with array from object in function, it impacts/changes also other object arrays.
Here is what I do. I describe object class:
Class testObject {
[array] $list
[int] $increment }
Then I create two objects that both have the same $list:
$numberList = (1,1,1,1,1,1,1,1,1)
$obj1 = New-Object testObject
$obj1.list = $numberList
$obj1.increment = 2
$obj2 = New-Object testObject
$obj2.list = $numberList
$obj2.increment = 5
Next I describe function that will increase each array item by the increment value specified in each object:
Function Increase-numbers ($obj) {
[array] $array = $obj.list
For ($i = 0; $i -lt 9; ++$i) {
$array[$i] += $obj.increment }
$obj.list = $array
return $obj
}
Then I run the function in which I pass object to function and update the object with function results:
$obj1 = Increase-numbers $obj1
Write-Host "$($obj1.list)"
$obj2 = Increase-numbers $obj2
Write-Host "$($obj2.list)"
I would expect that returned values would be: For $obj1 (increment value = 2): 333333333 For $obj2 (increment value = 5): 666666666
But it actually returns: 333333333 888888888
This happens because in the first function call when I change the array values, it automatically updates $obj2 array values as well.
Does anyone have an idea what am I doing wrong?