0

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.

saftargholi
  • 896
  • 1
  • 9
  • 25

2 Answers2

3
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 
Community
  • 1
  • 1
Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • thank you for your answer but this is not my answer . i wanna declare ref in class and use it out or class . – saftargholi Nov 13 '16 at 15:53
  • Sorry, I understood it in a wrong way. You've to call your `test` method with the **ref** keyword -> use `[tst]::test([ref]$test)` instead of `[tst]::test($test)`. – Moerwald Nov 14 '16 at 09:07
0

use [ref] when you use class :

class tst 
{

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

    }
}

$test = "ddd"
$test

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