Problem
When calling function f $array in code below (PowerShell v2), I am getting an error:
f : Cannot bind argument to parameter 'csvObject1' because it is null.
My Code
$hash1 = @{
dependent_name = 'ADDM-Fun-3';
}
$obj1 = New-Object -TypeName PSObject -Property $hash1
$array = [System.Collections.ArrayList]@( $obj1, $null )
function f () {
Param(
[parameter(Position=0, Mandatory=$true)]
[System.Collections.ArrayList]$array
)
"Hello"
}
f $array
Question
Why does Powershell do this? It seams to me to be a design flaw - but maybe I am not seeing the big picture.
Comments
I believe this error is occurring because of the second line in the ArrayList is $null. I am slightly shocked by this 'finding' because:
- It has taken my about 4 hours to track down the issue.
- This seams to imply that using strong type defintions in the function is a bad idea because it causes PowerShell to check every element in the array which is an unexepected overhead.
- If I remove
[System.Collections.ArrayList]from the function definition, the problem goes away.