views:

230

answers:

2

Hi,

is there a way to supress the return value (=Index) of an ArrayList in Powershell (using System.Collections.ArrayList) or should I use another class?

$myArrayList = New-Object System.Collections.ArrayList($null)
$myArrayList.Add("test")
Output: 0

thanks in advance Milde

+4  A: 

You can cast to void to ignore the return value from the Add method:

[void]$myArrayList.Add("test") 

Another option is to redirect to $null:

$myArrayList.Add("test") > $null
Keith Hill
Thank you, it works perfectly."Easy answer" for you, but I'm still learning :)
Milde
I removed that comment because it appeared snarky - it wasn't meant that way. I was going to give a short/easy answer first and then delve into it some more but then decided it wasn't necessary.
Keith Hill
+2  A: 

Two more options :)

Pipe to out-null

$myArrayList.Add("test") | Out-Null

Assign the result to $null:

$null = $myArrayList.Add("test")

Shay Levy