0

I am trying to pass an array to functions and fill it, and then print the results outside of the functions.
But the first function doesn't recognize the array list object I am passing to it.

Main file:

. $funcFile
$myParam = "Hello World"  
$myObj = getMyObject $myParam
$myObj.myArrayList.Count   # This works (outputs 0)
myFunction2 ($myObj.myArrayList)
$myObj.myArrayList.Count   # This also works (outputs 0)

fncFile:

function getMyObject([String] $myParam) {
    $myObj = @{  
         "myArrayList" = (New-Object System.Collections.ArrayList)  
    }
    return $myObj
}

function myFunction2 ([System.Collections.ArrayList] $myArr){
    $myArr.Count  # This doesn't work (outputs nothing)
    if($myArr -eq $null) {
         Write-Host "Array List Param is null"   # This condition is FALSE - nothing is printed
    }
}

What am I doing wrong?
How can I use the same ArrayList in function2 and other inner functions?

4
  • 1
    function1, function2 should be just function Commented Feb 4, 2019 at 9:00
  • Changed it. thanks Commented Feb 4, 2019 at 10:31
  • I can not reproduce described behavior on my PC. What PowerShell version did you use to reproduce the issue with given code? Commented Feb 4, 2019 at 10:38
  • @Davis8988 after the edit it print 3 zeros. Seems to be working to me Commented Feb 4, 2019 at 12:11

1 Answer 1

1

If you want to pass a variable and modify it in function and use the result there are 2 ways:

Pass by value:

$arr = New-Object System.Collections.ArrayList
function FillObject([System.Collections.ArrayList]$array, [String] $myParam) {
    return $array.Add($myParam)
}
$arr = FillObject -array $arr -myParam "something"
$arr.Count

Pass by reference (what you asked about)

[System.Collections.Generic.List[String]]$lst = (New-Object System.Collections.Generic.List[String]])
function FillObject([System.Collections.Generic.List[String]][ref]$list,[String] $myParam) {
    $list.Add($myParam)
}
FillObject -list ([ref]$lst) -myParam "something"
$lst.Count

You have to add [ref] both in the function definition and when you pass the parameter. If this will help you - Powershell and C# rely on .NET, so their syntax is similar. C# way to use the ref:

int number = 1;
void Method(ref int refArgument)
{
    refArgument = refArgument + 44;
}
Method(ref number);
Sign up to request clarification or add additional context in comments.

2 Comments

I tried what your wrote but it still doesn't work.. Besides, according to Microsoft documentation objects are passed by reference by default. Since ArrayList is an object, the passing of it is by reference in both of your examples if you include [ref] or not.
The thing is to be explicit about what you want. It helps in later stages of development.

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.