saftargholi saftargholi - 23 days ago 6
C# Question

how i can declare out in parameter in powershell like c#

I have c# code and i want to find alternative of this code in PowerShell . I found something like

[ref]$parameter
but it's doesn't work .
My code is :

private static bool testfunction(string param1, out string param 2)
{
param1 = "";
param2 += "Hello";
return true;
}


Please give me alternative code in PowerShell.

i try this :

class tst
{

static test([ref]$param)
{
$param.Value = "world "

}
}

$test = "ddd"
$test

[tst]::test($test)
$test


this is doesn't work.

Answer
function testfunction {
   param (
       [string]
       $param1,
       [ref]
       $param2
   )

   $param2.value= "World"
   return $true
}

PS C:\> $hello = "Hello"

PS C:\> testfunction "someString" ([ref]$hello)
True

PS C:\> $hello
World

Powershell supports ref parameters. Be sure to call the ref parameter in brackets (e.g. ([ref] $parameter). Be also be aware to only declare [ref]as type in the paramblock. Further details:

stackoverflow

ss64

hope that helps

UPDATE

You've to call your test method with the ref keyword -> use [tst]::test([ref]$test) instead of `[tst]::test($test)

PS C:\> $test = "ddd"

PS C:\> $test
ddd

PS C:\> [tst]::test([ref]$test)

PS C:\> $test
world