Reputation: 67
I'm building a menu and want to pass an array to a function as a reference.
When I'm passing just the reference everything works fine. But when I want to pass a second parameter, it won't work with an "ParameterBindingArgumentTransformationException". And I do not understand why.
Here is the - working- minimal code:
function Menu {
param([Ref]$mi)
Write-Host "var"
Write-Host $mi.value[0].cmd
Write-Host $mi.value[1].desc
Write-Output 0
}
function MainMenu {
$mm = @(@{desc='Windows Tools';cmd='WinToolsMenue'},
@{desc='copy setup.exe';cmd='setupexe'},
@{desc='gpupdate (Policy)';cmd='cmd /c gpupdate /force'},
@{desc='Exit';cmd='break'})
$a = Menu([Ref]$mm)
}
& MainMenu
The problem-code:
function Menu {
param([Ref]$mi, $b)
Write-Host $b
Write-Host $mi.value[0].cmd
Write-Host $mi.value[1].desc
Write-Output 0
}
function MainMenu {
$mm = @(@{desc='Windows Tools';cmd='WinToolsMenue'},
@{desc='copy setup.exe';cmd='setupexe'},
@{desc='gpupdate (Policy)';cmd='cmd /c gpupdate /force'},
@{desc='Exit';cmd='break'} )
$mm
$a = Menu([Ref]$mm, "b")
}
& MainMenu
Almost tried ([Ref]$mi), $b or ([Ref]$mi, $b), but it won't work. Someone out there who knows what I'm doing wrong?
Upvotes: 1
Views: 2400
Reputation: 32170
$a = Menu ([Ref]$mm, "b")
That's not how you call a function. Remember, the comma is an operator for arrays, and parentheses indicate a expression that gets evaluated first. That doesn't change just because you're calling a function. This says, "Call function Menu
, with the first parameter an array with two elements of [Ref]$mm
and "b"
. Essentially, you're calling this:
$a = Menu -mi @([Ref]$mm, "b") -b $null
You need to specify:
$a = Menu ([Ref]$mm) "b"
Or:
$a = Menu -mi ([Ref]$mm) -b "b"
Upvotes: 2