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
Answer from Keith Hill on Stack OverflowYou 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
Two more options :)
Pipe to out-null
$myArrayList.Add("test") | Out-Null
Assign the result to $null:
$null = $myArrayList.Add("test")
ArrayList.Add Method
powershell - Add an array to an array list - Stack Overflow
How to merge two ArrayList while keeping the Arraylist type?
Two ways of creating a new System.Collections.ArrayList object, what's the difference?
Videos
Hey guys
I am trying to add Table Objects into an ArrayList. This always worked but suddenly I am getting an error.
#create table class
class Table
{
[String] $table_name
[String] $content_type
[String] $finishedOn
}I add the Table-Objects like this:
$tables = New-Object System.Collections.ArrayList
while ($reader.Read())
{
$table = New-Object Table
$table.table_name = $reader["table_name"];
$table.content_type = $reader["content_type"];
$table.finishedOn = $LoadDate;
[void]$tables.Add($table);
}The error I'm getting is:
System.Management.Automation.RuntimeException Method invocation failed because [Table] does not contain a method named 'Add'.
I don't really get why PS gives me this error, since I'm using the Add-Method of the ArrayList, and am adding a Table-Object. As far as I know I don't need an add-Method for Table-Objects. Also it's very strange that I never got this error before while trying out my script, but now it's appearing every time.
Do I need to create an Add-Method for my Table-Class? And if so, how would I go about this?
EDIT: Fuck this. When I use Get-Member -InputObject $tables It sometimes tells me $tables is an array, sometimes it tells me it's an arraylist and sometimes it tells me it's a table. It's infuriating (though interesting). I'll try if I can pinpoint where it changes its object-type.
EDIT2: Thanks everyone! I'm really happy there is such a nice and active PowerShell community here on reddit. I hope I can come back to you if I got any other questions and as I'm learning more, hopefully I'll be able to answer some questions of other users! :)
I have 2 Arraylists. What I want to do is to merge them, but whatever I do the combined arraylist gets wrapped into Object[].
It does not make sense for my current uderstanding on the topic.
$first = New-Object System.Collections.ArrayList($null)
$second = New-Object System.Collections.ArrayList($null)
$combined = New-Object System.Collections.ArrayList($null)
[void]$first.add("I")
[void]$first.add("want to join")
[void]$first.add("the first arraylist")
[void]$second.add("and the second arraylist")
[void]$second.add("together")It gets wrapped into Object[]...
$combined.add($first) $combined.add($second)
The same with this method:
$combined += $first $combined += $second
I want to keep the original arraylist format and just add the elements within then into a 3rd arraylist. It's sad, but my google skills failed me...
I found many mentions of .AddAll method, but unfortunately it does not exist in PS :D
I've come across two ways of creating a new System.Collections.ArrayList object:
$arrA = New-Object System.Collections.ArrayList $arrB = [System.Collections.ArrayList]@()
I have two questions:
-
for arrB, if I'm reading this right, @() is creating an empty array and then it's being cast into [System.Collections.ArrayList]?
-
Working with the created object is the same for me either way, but are there any differences I may be missing?
I made a quick test and casting was faster, but the difference is only noticeable when creating new arrays reaches the hundreds of thousands to millions (tested on an old A10-7870k). Doesn't really matter when I'm creating one array, but I thought it was mildly interesting.
$max = 100000
$ticksStart = (Get-Date).Ticks
for ($i = 0; $i -lt $max; $i++)
{
$arrA = New-Object System.Collections.ArrayList
}
$ticksEnd = (Get-Date).Ticks
Write-Host 'arrA (ticks) : ' ($ticksEnd - $ticksStart)
$ticksStart = (Get-Date).Ticks
for ($i = 0; $i -lt $max; $i++)
{
$arrB = [System.Collections.ArrayList]@()
}
$ticksEnd = (Get-Date).Ticks
Write-Host 'arrB (ticks) : ' ($ticksEnd - $ticksStart)
arrA (ticks) : 64310000
arrB (ticks) : 3170000