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:-
- 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?
- Parameters
- Parameter:- ch
- It states that we are trying to convert “System.Object[]” to “System.Char”
- Parameter:- ch
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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.