View Single Post
Old 07-17-2006   #3 (permalink)
Bruce Payette [MSFT]


 
 

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