PowerShell associative array cannot be returned as function return value

Asked 1 years ago, Updated 1 years ago, 322 views

I'm creating a GUI that does a lot of things with PowerShell.
Because there are a number of processes that require associative arrays, the function for creating associative arrays is once sandwiched and returned as a return value.However, we are at a standstill when we return it as a return value.
The following is a brief description of only the relevant code.

#Associative Array Creation
Function makeHashTable{
    Param(
        param1,
        param2
    )
    $HashTable=[System.Collections.General.Dictionary [String, PSObject]]:new()
    for ($i by index number) {
        $value=@()
        foreach($vin$values){
            $str = Abbreviated
            $value+=$str
        }
        $HashTable.Add ($List[$i], $value)
    }
    # 1
    return$HashTable
}

# Button 2 Processing
Function Calc {
    Param(
        $HashTable
    )
    # 3
    $HashTable Processing
}

approximately
# ASSOCIATIVE ARRAYING BUTTON
$Button1 = Abbreviated
$Button1.Add_Click({makeHashTable param1param2})

# button for passing associative array
$Button2 = Abbreviated
$Button2.Add_Click({Calc$HashTable})#2

When you click button 1, an associative array is created, and when you click button 2, the associative array is used for processing.
What I tried was to call the associative array by adding Write-Host to parts 1, 2, and 3 to see if it would run without any problems (for 3, I rewritten Calc$HashTable to Write-Host$HashTable).
The result is
An associative array is created for
1. No associative array was created for 2 ($HashTable.Count is 0)
Same as
2 for 3 Yes,
I wonder why the return value of makeHashTable has not been returned from the above.
Also, how should it be corrected?
Thank you for your advice.

powershell

2022-12-05 09:03

1 Answers

I wonder why the return value of makeHashTable has not been returned.

I got it back, but I don't have the code to receive it.

Basically, I think you should understand the scope of the variable

Then, it is safe to define the $HashTable outside the function, in the same scope as $Button1 and $Button2.

#Associative Array Creation
Function makeHashTable{
    Param(
        param1,
        param2
    )
    for ($i by index number) {
        $value=@()
        foreach($vin$values){
            $str = Abbreviated
            $value+=$str
        }
        $HashTable.Add ($List[$i], $value)
    }
}

# Button 2 Processing
Function Calc {
    $HashTable Processing
}

approximately

# create associative sequences here
$HashTable=[System.Collections.General.Dictionary [String, PSObject]]:new()

# ASSOCIATIVE ARRAYING BUTTON
$Button1 = Abbreviated
$Button1.Add_Click({makeHashTable param1param2})

# button for passing associative array
$Button2 = Abbreviated
$Button2.Add_Click({Calc})

If this is the case, both makeHashTableCalc are available.


2022-12-05 09:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.