3

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
2
  • Why are you using [ref]$arr = @() instead of $arr = @() in the first place? Commented Sep 6, 2016 at 16:13
  • 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. Commented Sep 6, 2016 at 17:07

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

0

When using [ref] with params, we have to refer to the passed parameter via $parameter.Value instead of just $parameter:

function Foo-Bar {
    param (
        [ref]$array
    )
    Write-Host $array

    $array.Value += { Title="a" }
    
}

$externalArray = New-Object System.Collections.Generic.List[System.Object]
$externalArray += { Title="initial" }

Foo-Bar -a ([ref]$externalArray)

  • Works $array.Value += { Title="a" }
  • Does not work: $array+= { Title="a" }

Comments

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.