Retrieving a function name as a parameter within a function

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
  • #5445

    by ChrisL at 2013-01-16 14:16:25

    How do you retreive a parameter passed to a function as the name of another function (or an action item in general). For example:

    fnSomething "fnSomethingElse"
    fnSomething "$oForm.Close()"

    I've tried using get-variable but that doesn't seem to work:
    (Get-Variable $sVarName).Name or .Value

    by RichardSiddaway at 2013-01-16 14:29:23

    Have you tried invoke-expression
    function ffour {

    function fthree {

    function ftwo {

    Invoke-Expression $fname

    ftwo fthree

    ftwo ffour

    by ChrisL at 2013-01-17 10:31:47

    yes, it doesn't work... And I verified the strings are coming through correctly.

    What it is, is I'm creating a form-building function, and I can set actions (say on a button) manually but not if I put it in the function.

    So this for example works (Forgive these examples, it's from my crude test script where I'm testing individual bits):
    fnCheckBox "oCheckBox2" "oGroupBox0" @("20","322") @("200","22") "Dev"
    $oCheckBox2.Add_CheckedChanged({ if($oCheckBox2.Checked) {fnClearRadio}})

    But this isn't:
    Function fnButton ($sVarName, $sParent, [int[]]$aPos, [int[]]$aSize, $sTxt, [array]$aAction) {
    $Object = New-Object System.Windows.Forms.Button
    $Object.Location = New-Object System.Drawing.Size("$aPos[0]","$aPos[1]")
    $Object.Size = New-Object System.Drawing.Size("$aSize[0]","$aSize[1]")
    $Object.Text = $sTxt
    #Actions to be taken on click (For ENTER see oForm)
    ForEach ($Item in $aAction){
    Write-Host $Item #Verification
    If ($sAction -ne $null){ $sAction + "; Invoke-Expression $Item" }
    Else { $sAction = "Invoke-Expression $Item" }
    New-Variable -Name $sVarName -Value $Object -Scope Script
    If ($sParent -ne $null) { (Get-Variable $sParent).value.Controls.Add((Get-Variable $sVarName).value) }
    fnButton "oClearButton" @("100","400") @("100","20") "Clear Selection" @("fnClearCheck","fnClearRadio")

    Those aren't for the same item obviously...

    by DonJ at 2013-01-17 15:41:49

    I'm not in front of a shell at the moment, but & is part of your answer. Something like &FunctionName... really sorry, on mobile right now, but it's the invocation operator you want. That forces the "execution of the string" rather than the string itself. What you're doing in your above "this isn't" example is still just putting a command in a string. PowerShell won't execute strings. $sAction = { Invoke-Expression } would be the correct syntax – assign a script block containing the command, not a string.

    by ChrisL at 2013-01-18 22:03:09

    Thanks for the reply Don (especially while on the go) but I'm confused...

    If I run this It does nothing, but $sAction at least looks correct:
    ForEach ($Item in $aAction){
    If ($sAction -ne $null){ $sAction + "; $Item" }
    Else { $sAction = "$Item" }
    $Object.Add_Click({(& $sAction)})

    And if I run this, $sAction just equals $Item, not the variable, but the name:
    ForEach ($Item in $aAction){
    If ($sAction -ne $null){ $sAction + {;$Item} }
    Else { $sAction = {$Item} }
    $Object.Add_Click({(& $sAction)})

    In both cases $sAction comes up as an invalid pipeline element. (I would imagine my addition mechanism is incorrect but it should still work on a single action)

    by ChrisL at 2013-01-22 10:58:01

    I tried passing a script block as well, but I'm not sure if I'm doing it right; either way I got the same result:

    fnButton "oOKButton" "oForm" @("40","235") @("75","20") "OK" {fnSelectionInstall;$oForm.Close()}
    fnButton "oClearButton" "oForm" @("116","235") @("100","20") "Clear Selection" {fnClear1234;fnClear56}
    fnButton "oCancelButton" "oForm" @("217","235") @("75","20") "Cancel" {$oForm.Close()}

You must be logged in to reply to this topic.