Author Posts

February 19, 2016 at 9:08 pm

HI
I have a problem with arraylist, that is the added items are being overwritten with the newly added item.
check the code below

$Finalresult=@()
$Object = New-Object PSObject

$Object | add-member Noteproperty LineNumber -Value ""
$Object | add-member Noteproperty Date -Value ""
$Object | add-member Noteproperty ServerName -Value ""
$Object | add-member Noteproperty DatabaseName -Value ""

for ($i=0;$i -le 3;$i++){
Write-Host "adding for $i time" -ForegroundColor Green

$Object.DatabaseName=Read-Host "DatabaseName"
$Object.Date=read-host "date"
$Object.LineNumber=Read-Host "linenumber"
$Object.ServerName=read-host "Servername"
$Finalresult +=$Object

Write-Host "Object value are as the following" -ForegroundColor Green -NoNewline
Write-Host $Object
Write-Host ""
Write-Host "The value in Finalresult are" -ForegroundColor Green
$Finalresult
}
When I add the new item $object to the array $finalresult, all the previous items added to $finalresult are overwritten with the new added item and also it add extra item

LineNumber Date ServerName DatabaseName
———- —- ———- ————
Value4 Value4 Value4 Value4
Value4 Value4 Value4 Value4
Value4 Value4 Value4 Value4
Value4 Value4 Value4 Value4

what is going on.. I hope I was able to explain the problem.

February 19, 2016 at 9:22 pm

You're only creating your $Object variable once, and overwriting its properties every time through the loop. Your arraylist just contains the same object reference, over and over again.

The fix is to move your call to New-Object inside the loop.

February 19, 2016 at 9:40 pm

Oh,. it work 🙂
But Let me please understand something.
during the loop (when the new-object is outside the loop), I am adding the new value to $object properties and then add the $object to the arraylist.
during the loop the $Object properties are getting updated with a new user input, and these input are added to Arraylist
so why the overwirte is happening
Let me explain in example so
in the first loop $object properties were "Value1", and then I added these value to the array (they should stay there as they are already added)
In the second loop $object properties were "Value2", when adding the new $object properties to the array, why the overwrite.. its adding new!

but it work and I will be happy if you can explain a bit more, and sorry for my weak english

Thanks

February 20, 2016 at 6:13 pm

That is some very inefficient code. Appending to the $finalresults array is one of the least efficient things PowerShell does since the array has to be recreated on each pass.

Try this instead. Enter a blank line for the database to jump out of the input loop.

$i = 0
$results = while ($true) {
    Write-Verbose "$("Entry {0:D3} $("-"*32)" -f ++$i)"
    $db = Read-Host "DatabaseName"
    if (! $db) { break }
    $date = Read-Host "date"
    $ln  = Read-Host "linenumber"
    $server = Read-Host "Servername"
    [PSCustomObject]@{
        Database = $db
        Date = $date
        LineNumber = $ln
        ServerName = $server
    }
    Write-Host
}
$results

February 24, 2016 at 5:12 am

Faris, you do not add new object into array, you add new reference to the same object into array...
short and dirty: objects is a references to data, but not data