Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Problem Calling String(char[] value) constructor

Reply
 
Old 07-16-2006   #1 (permalink)
Keith Hill [MVP]
Guest


 
 

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
Old 07-16-2006   #2 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=
Guest


 
 

RE: Problem Calling String(char[] value) constructor

I was also playing aroudn to create a new string object through other
constructor with signature, "public String (char[] value, int startIndex, int
length)"

It was painfully tough to get around to pass arguments for startIndex and
Length...

Where, $a is an Character array of "Hello World" string.
[^_^]PS[148]>new-object String(,$a)
Hello World
[^_^]PS[149]>new-object String((,$a), 0, 5) #expecting "Hello"
New-Object : Cannot convert argument "0", with value: "System.Object[]", for
".
ctor" to type "System.Char[]": "Cannot convert "System.Char[]" to
"System.Char"
.."
At line:1 char:11
+ new-object <<<< String((,$a), 0, 5) #expecting "Hello"
[^_^]PS[150]>New-Object string (& { $OFS = ''; $p }) 0 5
New-Object : A parameter cannot be found that matches argument ''.
At line:1 char:42
+ New-Object string (& { $OFS = ''; $p }) 0 <<<< 5

# this works.
[^_^]PS[151]>New-Object string (& { $OFS = ''; $p }), 0, 5
Hello

I had actually expected "150"th command to work instead of 151st one though.
My System SpecsSystem Spec
Old 07-17-2006   #3 (permalink)
Bruce Payette [MSFT]
Guest


 
 

Re: Problem Calling String(char[] value) constructor

The new-object cmdlet is a placeholder until we get around to adding a
proper "new" keyword. It's certainly harder to use than it should be.
Unfortunately using it like:

new-object String($x,1,3)

obscures the fact that it's a cmdlet making things even more confusing. The
syntax for new-object is

New-Object [-TypeName] <String> [[-ArgumentList] <Object[]>]

so the above is really

new-object -typename string -argumentlist $x,1,3

The comma notation indicates an argument that is passed as an array. This is
equivalent to

$constructor_arguments= $x,1,3
new-object string $constructor_arguments

Note that we're not wrapping $constructor_arguments in yet another array. If
you want to pass an array as a single value, you need to do it yourself and
write it in parens with the unary comma operator.

Let's look at some examples. We want to construct a string from a char array
with offset and length:

PS (27) > new-object string ([char[]] "Hello"),1,4
ello

This is very straightforward. And if we put the array in a variable first,
it's even simpler:

PS (28) > $str = [char[]] "Hello"
PS (29) > new-object string $str,1,4
ello

The tricky one is passing the char array as a single argument. Now we need
to wrap it up:

PS (30) > new-object string (,$str)
Hello

It would have been an arguably better design for new-object to have taken a
variable number of arguments instead of passing them all as a single array.
The current design matches the activator APIs:

PS (32) > [activator]::CreateInstance([string],[char[]] "Hello")
Hello
PS (33) > [activator]::CreateInstance([string],([char[]] "Hello",1,3))
ell

You could work around some of the deficiencies of the cmdlet with a
function:

PS (61) > function new
>> {
>> param ($type, [switch] $com, [switch] $strict)
>> if ($com)
>> {
>> new-object -com -strict:$strict $type $args
>> }
>> else
>> {
>> new-object $type $args
>> }
>> }
>>

PS (62) > new string ([char[]] "Hello")
Hello
PS (63) > new string ([char[]] "Hello") 2 3
llo
PS (64) >

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



"dance2die" <dance2die@discussions.microsoft.com> wrote in message
news:112AA279-EB3E-4FE7-880D-882801A44856@microsoft.com...
>I was also playing aroudn to create a new string object through other
> constructor with signature, "public String (char[] value, int startIndex,
> int
> length)"
>
> It was painfully tough to get around to pass arguments for startIndex and
> Length...
>
> Where, $a is an Character array of "Hello World" string.
> [^_^]PS[148]>new-object String(,$a)
> Hello World
> [^_^]PS[149]>new-object String((,$a), 0, 5) #expecting "Hello"
> New-Object : Cannot convert argument "0", with value: "System.Object[]",
> for
> ".
> ctor" to type "System.Char[]": "Cannot convert "System.Char[]" to
> "System.Char"
> ."
> At line:1 char:11
> + new-object <<<< String((,$a), 0, 5) #expecting "Hello"
> [^_^]PS[150]>New-Object string (& { $OFS = ''; $p }) 0 5
> New-Object : A parameter cannot be found that matches argument ''.
> At line:1 char:42
> + New-Object string (& { $OFS = ''; $p }) 0 <<<< 5
>
> # this works.
> [^_^]PS[151]>New-Object string (& { $OFS = ''; $p }), 0, 5
> Hello
>
> I had actually expected "150"th command to work instead of 151st one
> though.



My System SpecsSystem Spec
Old 07-17-2006   #4 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: Problem Calling String(char[] value) constructor

"Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message
news:%23ErYVKdqGHA.2068@TK2MSFTNGP03.phx.gbl...
> The new-object cmdlet is a placeholder until we get around to adding a
> proper "new" keyword. It's certainly harder to use than it should be.
> Unfortunately using it like:
>
> new-object String($x,1,3)
>
> obscures the fact that it's a cmdlet making things even more confusing.
> The syntax for new-object is
>
> New-Object [-TypeName] <String> [[-ArgumentList] <Object[]>]
>


Thanks for that explanation. I've got to quit thinking in terms of C#
method calls with parens around the parameter list! BTW a "new" keyword
would be very nice if it allowed PoSH to be smarter about interpreting
constructor arguments.

--
Keith


My System SpecsSystem Spec
Old 07-17-2006   #5 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=
Guest


 
 

Re: Problem Calling String(char[] value) constructor

Thank you for the great explanations there.



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Get single char from string by position ? VB Script
intermittent workstation trust error calling IsInRole(string) .NET General
Re: Extracting the last char from the string VB Script
Re: how to count number of certain char within string PowerShell
Constructor definition PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46