View Single Post
Old 07-16-2006   #1 (permalink)
Keith Hill [MVP]


 
 

Problem Calling String(char[] value) constructor

Invoking the String(char[] value) constructor is a bit hard in PowerShell e.g.:

1 > $a = "Hello World".ToCharArray()
2 > $a
H
e
l
l
o

W
o
r
l
d
3 > $str = new-object String($a)
New-Object : Cannot find an overload for ".ctor" and the argument count: "11".
At line:1 char:18
+ $str = new-object <<<< String($a)
4 > $str = new-object String $a
New-Object : Cannot find an overload for ".ctor" and the argument count: "11".
At line:1 char:18
+ $str = new-object <<<< String $a
5 > $str = new-object String ,$a
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' requi
red by parameter 'TypeName'. Specified method is not supported.
At line:1 char:18
+ $str = new-object <<<< String ,$a
6 > $str = new-object String(,$a)
7 > $str
Hello World

Finally got it to work. But why doesn't the more obvious "new-object String($a)" work? Why is PoSH expanding the array into 11 chars when it's a parameter that is supposed to be an array? Also, having to use the ",<array>" syntax can be a tad confusing when used in the middle of a comma separated parameter list.

--
Keith
My System SpecsSystem Spec