Why the all the arraylist values are overwritten

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 7 months, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #35490
    Profile photo of Faris
    Faris
    Participant

    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.

    #35491
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

    #35492
    Profile photo of Faris
    Faris
    Participant

    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

    #35497
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    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
    
    #35618
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.