Why the all the arraylist values are overwritten

Welcome Forums General PowerShell Q&A Why the all the arraylist values are overwritten

This topic contains 4 replies, has 4 voices, and was last updated by

3 years, 4 months ago.

  • Author
  • #35490

    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

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

    $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
    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

    Topics: 9
    Replies: 2322
    Points: 0
    Rank: Member

    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

    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

    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


  • #35497

    Topics: 6
    Replies: 236
    Points: 0
    Rank: Member

    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"
            Database = $db
            Date = $date
            LineNumber = $ln
            ServerName = $server
  • #35618

    Topics: 2
    Replies: 376
    Points: 0
    Rank: Member

    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

The topic ‘Why the all the arraylist values are overwritten’ is closed to new replies.