2
votes

I have an array object variable as [ref] $InsertColHeadName = @() in a function and then call another function with param ([ref] $InsertColHeadName). In the called function I then tried refering to my parameter set in param ([ref] $InsertColHeadName) += expression. The expression returns a string. I placed a breakpoint in the line and tried forcing a string with single quotes eg.: ([ref] $InsertColHeadName) += 'xyz';.

I tried googling around but can't seem to find a suitable solution.

I am getting the following error:

Method invocation failed because [System.Management.Automation.PSReference`1
[[System.Management.Automation.PSReference`1
[[System.Management.Automation.PSReference`1
[[System.Object[], mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]], System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Management.Automation, 
Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]] does not
contain a method named 'op_Addition'.
At line:1 char:1
+ ([ref] $InsertColHeadName) += 'DepartmentNo';
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
3
Why are you using [ref]$arr = @() instead of $arr = @() in the first place?Ansgar Wiechers
Before I was using it inside the main function and it did not need the reference. As the main function uses a particular logic a couple of times that is the same that has some validation logic I thought it would be better to push it to a function and call the function. The variable I am trying to reference is used in another function call that may need the variable to be available.Costa Zachariou

3 Answers

2
votes

Inside the function the InsertColHeadName is already a reference. Adding [ref] to it creates a reference of the reference.

instead try :

$InsertColHeadName.Value += expression

Here is an example:

function AddMe([ref]$array)
{
    $array.value += 40
}

$x = @(1..10)

Write-Host Before Calling AddMe $x
AddMe ([ref]$x)
Write-Host After Calling AddMe $x

The output:

Before Calling AddMe 1 2 3 4 5 6 7 8 9 10
After Calling AddMe 1 2 3 4 5 6 7 8 9 10 40
0
votes

Still confused on how to do it with the array set as $InsertColHeadName = @() but went around it the following way.

In the main function I created the array as Array = New-Object System.Collections.Generic.List[System.Object]. When calling the function I passed the parameter as ([ref] $InsertColHeadName).

Inside the calling function in param I set up the parameter as [ref] $InsColHeadName as a new name as to not mix it up with the main function to make maintenence a little easier and then added the value to the array by doing ($InsColHeadName.Value).Add(...);.

The above made the passing by reference to work when it returned back to the main function.

0
votes

Had this same issue. Just have to make sure you do not cast the object to [ref] multiple times. It's like casting [ref][ref][ref]$YourObject.