![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #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 Specs![]() |
| | #4 (permalink) |
| | 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 ! > > 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 Specs![]() |
![]() |
| 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 | |||