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 - preventing PS from Casting object to an array

Reply
 
Old 01-30-2008   #1 (permalink)


various
 
 

preventing PS from Casting object to an array

Hi,

I have a function that returns a Generic collection (List<T> or whatever).

PS insist on casting the object into an array (if it contains items), a single object or an array.
I don't what this cast to occur as I want to use the object as is (for instance a List<Sting> should not be casted into a $null, string or string[]).

Worse a DataSqlReader that contains rows gets cast as null upon function return:

function GetDataReader($sqlConnection, $storedProcedureName)
{
# no check, no dispose, ... testing script only
$sqlCommand = new-object "System.Data.SqlClient.SqlCommand" $storedProcedureName
$sqlCommand.Connection = $sqlConnection
$sqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure
$retVal = $sqlCommand.ExecuteReader()
# The following display the expected type.
Write-Warning $retVal.GetType().Name
#also tried to return by explicitely using the "return" keyword, without it (as below), explicit casting, ... no luck.
$retVal
}

$dataSqlData = GetDataReader $sqlConn $storeProcName
if( $dataSqlData -eq $null)
{
Write-Warning "Why this Lame cast ?"
}

I probably miss something ...
Is there a way to prevent PS from doing this ?
(Haven't tried [ref] yet, may work but I rather have a return value)

My System SpecsSystem Spec
Old 01-30-2008   #2 (permalink)
/\/\o\/\/ [MVP]


 
 

RE: preventing PS from Casting object to an array

The arry get's enumerated by the pipeline you can prevent this by wrapping
the retrun value in another array :

Return @(,$retVal)

Greetings /\/\o\/\/

"Marclown" wrote:
Quote:

>
> Hi,
>
> I have a function that returns a Generic collection (List<T> or
> whatever).
>
> PS insist on casting the object into an array (if it contains items), a
> single object or an array.
> I don't what this cast to occur as I want to use the object as is (for
> instance a List<Sting> should not be casted into a $null, string or
> string[]).
>
> Worse a DataSqlReader that contains rows gets cast as null upon
> function return:
>
> function GetDataReader($sqlConnection, $storedProcedureName)
> {
> # no check, no dispose, ... testing script only
> $sqlCommand = new-object \"System.Data.SqlClient.SqlCommand\"
> $storedProcedureName
> $sqlCommand.Connection = $sqlConnection
> $sqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure
> $retVal = $sqlCommand.ExecuteReader()
> # The following display the expected type.
> Write-Warning $retVal.GetType().Name
> #also tried to return by explicitely using the \"return\" keyword,
> without it (as below), explicit casting, ... no luck.
> $retVal
> }
>
> $dataSqlData = GetDataReader $sqlConn $storeProcName
> if( $dataSqlData -eq $null)
> {
> Write-Warning "Why this Lame cast ?"
> }
>
> I probably miss something ...
> Is there a way to prevent PS from doing this ?
> (Haven't tried [ref] yet, may work but I rather have a return value)
>
>
> --
> Marclown
>
My System SpecsSystem Spec
Old 01-31-2008   #3 (permalink)


various
 
 

Re: preventing PS from Casting object to an array

Weird that you have to get out of your way to trick PS but - hey - that works
Thx !
My System SpecsSystem Spec
Old 01-31-2008   #4 (permalink)
Karl Prosser[MVP]


 
 

Re: preventing PS from Casting object to an array

Marclown wrote:
Quote:

> Weird that you have to get out of your way to trick PS but - hey - that
> works
> Thx !
>
>
just be aware that every place it gets pushed into a filter or a
pipeline its going to be stripped.

,,,(1..5) | % { $_ }
,,,(1..5) | % { $_ } | % { $_ }
,,,(1..5) | % { $_ } | % { $_ } | % { $_ }

to see how the wrapping the array within an array within an array etc
gets stripped one lever at each pipeline boundary..

If there is risk of such thing, maybe its best to instead wrap your
generic collection in a hashtable , or custom object.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Writing an array back in to an AD Object PowerShell
array or object??? PowerShell
Creating an array of floats using new-object PowerShell
New-object preventing process from exiting 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