1

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?

1 Answer 1

2
$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"
Sign up to request clarification or add additional context in comments.

1 Comment

ok, got it. Thank you for your replay. References need to be evaluated and comma is an array separator, that was my fault. Will remember this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.