Powershell:- Function – Invocation – Error – ParameterArgumentTransformationError

Background

Last evening I wanted to make a function out of a piece of Powershell Code Block, but ran into an error.

Let us quickly go over the error.

 

Script

Code


[string] $title;
[char]   $ch;
[int]    $len=0;
[string] $filler = "";
[boolean] $bCallFunctionWithComma = $false;

function repeatChar([char] $ch,[int] $n)
{

   [String] $str = "";
   
   $str = [String]::new( $ch, $n);
    
   return ( $str); 
     
}

$ch = '=';

$title = 'Head of the class';

$len = $title.length;

$bCallFunctionWithComma = $true;
#$bCallFunctionWithComma = $false;

if ($bCallFunctionWithComma -eq $true)
{
    
    $filler = repeatChar( $ch, $len);

}
else
{
    
    $filler = repeatChar -ch $ch -n $len;

}

Write-Host $title;
Write-Host $filler;


 

Error

Error – Image

Error – Text


repeatChar : Cannot process argument transformation on parameter 'ch'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Char".
At repeatChar.powershell.eitherOr.ps1:30 char:25
+     $filler = repeatChar( $ch, $len);
+                         ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [repeatChar], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,repeatChar


Error – Explanation

The error reads:-

  1. Cannot process argument transformation on parameter ‘ch’. Cannot convert the “System.Object[]” value of type “System.Object[]” to type “System.Char”
    • CategoryInfo : InvalidData: (:) [repeatChar], ParameterBindingArgumentTransformationException
    • FullyQualifiedErrorId : ParameterArgumentTransformationError,repeatChar

The error is a mouthful.

But, what does it mean?

  1. Parameters
    • Parameter:- ch
      • It states that we are trying to convert “System.Object[]” to “System.Char”

Where you ask?

The code reads:-

repeatChar( $ch, $len);

In powershell when we use a comma between arguments, we are requesting that the arguments be treated as a whole; an array of objects.

They are made as one.

In our case, $ch which is a character, and $len which is an integer coalesce into an object array.

 

Remediation

Code Change

Please change

$filler = repeatChar( $ch, $len);

to

$filler = repeatChar( $ch $len);

 

Code Snippet


[string] $title;
[char]   $ch;
[int]    $len=0;
[string] $filler = "";
[boolean] $bCallFunctionWithComma = $false;

function repeatChar([char] $ch,[int] $n)
{

   [String] $str = "";
   
   $str = [String]::new( $ch, $n);
    
   return ( $str); 
     
}

$ch = '=';

$title = 'Head of the class';

$len = $title.length;

#$bCallFunctionWithComma = $true;
$bCallFunctionWithComma = $false;

if ($bCallFunctionWithComma -eq $true)
{
    
    $filler = repeatChar( $ch, $len);

}
else
{
    
    $filler = repeatChar -ch $ch -n $len;

}

Write-Host $title;
Write-Host $filler;


 

Source Control

GitHub

Gist

DanielAdeniji/repeatChar.powershell.function.eitherOr.ps1

Link

 


[string] $title;
[char] $ch;
[int] $len=0;
[string] $filler = "";
[boolean] $bCallFunctionWithComma = $false;
function repeatChar([char] $ch,[int] $n)
{
[String] $str = "";
$str = [String]::new( $ch, $n);
return ( $str);
}
$ch = '=';
$title = 'Head of the class';
$len = $title.length;
$bCallFunctionWithComma = $true;
#$bCallFunctionWithComma = $false;
if ($bCallFunctionWithComma -eq $true)
{
$filler = repeatChar( $ch, $len);
}
else
{
$filler = repeatChar ch $ch n $len;
}
Write-Host $title;
Write-Host $filler;

 

Summary

As I have been saying a bit lately, programming languages are idiomatic in a way.

That is Programming Languages consider control characters on a cultural basis.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s