I want do create a custom object with a method that takes more than one parameter. I already managed to add a method that takes one parameter, and it seems to work:
function createMyObject () {
$instance = @{}
add-member -in $instance scriptmethod Foo {
param( [string]$bar = "default" )
echo "in Foo( $bar )"
}
$instance
}
$myObject = createMyObject
But every time I try to add a method with takes two parameters by simply adding another [string]$secondParam - clause to the param-section of the method, the invocation
$myObject.twoParamMethod( "firstParam", "secondParam" )
does not work. The error message in my language says something like "it is not possible to apply an index to a NULL-array".
Any hints? Thanks!
Something like this seems to work (at least in PowerShell v4)...
add-member -in $instance scriptmethod Baz {
param( [string]$bar = "default", [string]$qux = "auto" )
echo "in Baz( $bar, $qux )"
}
To call it, I did:
[PS] skip-matches> $myObject.Baz("Junk","Stuff")
in Baz( Junk, Stuff )
[PS] skip-matches> $myObject.Baz()
in Baz( default, auto )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With