Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to return a specific type from a function

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-07-2007   #1 (permalink)
Janssen
Guest


 

How to return a specific type from a function

Hello. I've written a routine that writes an XML file into a Data Table
([system.Data.DataTable]), and it performs exactly the way I want it to, but
when i put the routine inside a function and try to return the table at the
end by using "return $table", it loses the datatype of
[system.Data.DataTable]. It becomes an array. I can verify this by running
$table.gettype() before and after it exits the function. Before the return,
it says System.Data.DataRow. After the return, it says System.Array. I've
tried forcing it by saying [System.Data.Table]$table = loadtable but it
errors with the following message:
Cannot convert "System.Object[]" to "System.Data.DataTable".
You cannot call a method on a null-valued expression.

So really, my question is, in Powershell, can you tell a function to return
it's data as a specific data type? Any help would be appreciated.

Thanks!

Janssen Jones


My System SpecsSystem Spec
Old 02-07-2007   #2 (permalink)
Ryan Milligan
Guest


 

Re: How to return a specific type from a function

If your return value is getting wrapped in an array, you may be returning
multiple values. I just tried creating a function that simply returned
"new-object System.Data.DataTable", and that worked as expected...calling
GetType() on the return value returned DataTable. Have you poked through the
array that's getting returned and made sure there's only one thing in it?
Due to the way Powershell manages interaction with the pipeline, it's pretty
easy to accidentally return something that you just meant to calculate and
store. Any expression that returns a value that you invoke without storing
the result ends up on the return pipeline, not just the last expression in
the function -- the return statement is actually optional. Try invoking the
function "function foo { 'bar';'baz'; }", and you'll see what I mean. Try
printing out the Length property of the return value and see what it says.
If it says 1, I'd be very interested in seeing your script...if you post it,
or a simplification of it that still shows the behaviour you're seeing if
it's long or confidential, maybe someone can figure it out. Hope this helps.

-- Ryan Milligan

"Janssen" <Janssen@discussions.microsoft.com> wrote in message
news:EB5299B2-446A-4E74-B90B-1C62844464FA@microsoft.com...
> Hello. I've written a routine that writes an XML file into a Data Table
> ([system.Data.DataTable]), and it performs exactly the way I want it to,
> but
> when i put the routine inside a function and try to return the table at
> the
> end by using "return $table", it loses the datatype of
> [system.Data.DataTable]. It becomes an array. I can verify this by
> running
> $table.gettype() before and after it exits the function. Before the
> return,
> it says System.Data.DataRow. After the return, it says System.Array.
> I've
> tried forcing it by saying [System.Data.Table]$table = loadtable but it
> errors with the following message:
> Cannot convert "System.Object[]" to "System.Data.DataTable".
> You cannot call a method on a null-valued expression.
>
> So really, my question is, in Powershell, can you tell a function to
> return
> it's data as a specific data type? Any help would be appreciated.
>
> Thanks!
>
> Janssen Jones
>



My System SpecsSystem Spec
Old 02-07-2007   #3 (permalink)
Janssen
Guest


 

RE: How to return a specific type from a function

Thanks for the info. I'm trying to return 17 rows of a table completely
intact to be used as the same table outside of the function. What i notice
happening is that when it leaves the function, it still looks like everything
is there, but i no longer have access to table.rows.count. It is functioning
like an array instead of a datatable. Here's my function:
function load-table
{
[xml]$subnets = gc ~\Desktop\halls.xml
$table = New-Object system.Data.DataTable "subnets"
$col1 = New-Object system.data.DataColumn building
$col2 = New-Object system.data.DataColumn oc1, ([int])
$col3 = New-Object system.data.DataColumn oc2, ([int])
$col4 = New-Object system.data.DataColumn oc3, ([int])
$col5 = New-Object system.data.DataColumn oc4, ([int])
$col6 = New-Object system.data.DataColumn range
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$i=0
$j= $subnets.subnets.subnet.length
$row = @()
while ($i -lt $j)
{
[array]$row += ""
$row[$i] = $table.newrow()
$row[$i].building = $subnets.subnets.subnet[$i].building
$row[$i].oc1 = $subnets.subnets.subnet[$i].octet1
$row[$i].oc2 = $subnets.subnets.subnet[$i].octet2
$row[$i].oc3 = $subnets.subnets.subnet[$i].octet3
$row[$i].oc4 = $subnets.subnets.subnet[$i].octet4
$row[$i].range = $subnets.subnets.subnet[$i].sub
$table.rows.add($row[$i])
$i++
}
Write-Host $table.rows.count
return $table
}

$table
$table.rows.count

$table.rows.count returns "17" inside the function, but nothing outside the
function. In fact, though all the data is still there, $table.rows no longer
exists.

I hope that makes some sense.

Thanks for your help.

Janssen



"Janssen" wrote:

> Hello. I've written a routine that writes an XML file into a Data Table
> ([system.Data.DataTable]), and it performs exactly the way I want it to, but
> when i put the routine inside a function and try to return the table at the
> end by using "return $table", it loses the datatype of
> [system.Data.DataTable]. It becomes an array. I can verify this by running
> $table.gettype() before and after it exits the function. Before the return,
> it says System.Data.DataRow. After the return, it says System.Array. I've
> tried forcing it by saying [System.Data.Table]$table = loadtable but it
> errors with the following message:
> Cannot convert "System.Object[]" to "System.Data.DataTable".
> You cannot call a method on a null-valued expression.
>
> So really, my question is, in Powershell, can you tell a function to return
> it's data as a specific data type? Any help would be appreciated.
>
> Thanks!
>
> Janssen Jones
>

My System SpecsSystem Spec
Old 02-07-2007   #4 (permalink)
Ryan Milligan
Guest


 

Re: How to return a specific type from a function

Oh, I see the problem. DataTable implements IEnumerable, so PSH flattens it
and returns its contents on the pipeline instead of the collection itself.
To avoid this, simply put a comma in front of the value you want to return,
like so:

return ,$table;

This essentially creates an array that contains only $table -- PSH only
flattens one level of enumerables, so $table is passed directly on the
pipeline instead of its contents.

-- Ryan Milligan

"Janssen" <Janssen@discussions.microsoft.com> wrote in message
news:C20EF777-C515-45CB-BFB6-436C09FC8222@microsoft.com...
> Thanks for the info. I'm trying to return 17 rows of a table completely
> intact to be used as the same table outside of the function. What i
> notice
> happening is that when it leaves the function, it still looks like
> everything
> is there, but i no longer have access to table.rows.count. It is
> functioning
> like an array instead of a datatable. Here's my function:
> function load-table
> {
> [xml]$subnets = gc ~\Desktop\halls.xml
> $table = New-Object system.Data.DataTable "subnets"
> $col1 = New-Object system.data.DataColumn building
> $col2 = New-Object system.data.DataColumn oc1, ([int])
> $col3 = New-Object system.data.DataColumn oc2, ([int])
> $col4 = New-Object system.data.DataColumn oc3, ([int])
> $col5 = New-Object system.data.DataColumn oc4, ([int])
> $col6 = New-Object system.data.DataColumn range
> $table.columns.add($col1)
> $table.columns.add($col2)
> $table.columns.add($col3)
> $table.columns.add($col4)
> $table.columns.add($col5)
> $table.columns.add($col6)
> $i=0
> $j= $subnets.subnets.subnet.length
> $row = @()
> while ($i -lt $j)
> {
> [array]$row += ""
> $row[$i] = $table.newrow()
> $row[$i].building = $subnets.subnets.subnet[$i].building
> $row[$i].oc1 = $subnets.subnets.subnet[$i].octet1
> $row[$i].oc2 = $subnets.subnets.subnet[$i].octet2
> $row[$i].oc3 = $subnets.subnets.subnet[$i].octet3
> $row[$i].oc4 = $subnets.subnets.subnet[$i].octet4
> $row[$i].range = $subnets.subnets.subnet[$i].sub
> $table.rows.add($row[$i])
> $i++
> }
> Write-Host $table.rows.count
> return $table
> }
>
> $table
> $table.rows.count
>
> $table.rows.count returns "17" inside the function, but nothing outside
> the
> function. In fact, though all the data is still there, $table.rows no
> longer
> exists.
>
> I hope that makes some sense.
>
> Thanks for your help.
>
> Janssen
>
>
>
> "Janssen" wrote:
>
>> Hello. I've written a routine that writes an XML file into a Data Table
>> ([system.Data.DataTable]), and it performs exactly the way I want it to,
>> but
>> when i put the routine inside a function and try to return the table at
>> the
>> end by using "return $table", it loses the datatype of
>> [system.Data.DataTable]. It becomes an array. I can verify this by
>> running
>> $table.gettype() before and after it exits the function. Before the
>> return,
>> it says System.Data.DataRow. After the return, it says System.Array.
>> I've
>> tried forcing it by saying [System.Data.Table]$table = loadtable but it
>> errors with the following message:
>> Cannot convert "System.Object[]" to "System.Data.DataTable".
>> You cannot call a method on a null-valued expression.
>>
>> So really, my question is, in Powershell, can you tell a function to
>> return
>> it's data as a specific data type? Any help would be appreciated.
>>
>> Thanks!
>>
>> Janssen Jones
>>



My System SpecsSystem Spec
Old 02-07-2007   #5 (permalink)
RichS
Guest


 

Re: How to return a specific type from a function

Theres a similar issue in this thread that may be of interest

http://www.microsoft.com/communities...a-25ec4df4014d

--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Ryan Milligan" wrote:

> Oh, I see the problem. DataTable implements IEnumerable, so PSH flattens it
> and returns its contents on the pipeline instead of the collection itself.
> To avoid this, simply put a comma in front of the value you want to return,
> like so:
>
> return ,$table;
>
> This essentially creates an array that contains only $table -- PSH only
> flattens one level of enumerables, so $table is passed directly on the
> pipeline instead of its contents.
>
> -- Ryan Milligan
>
> "Janssen" <Janssen@discussions.microsoft.com> wrote in message
> news:C20EF777-C515-45CB-BFB6-436C09FC8222@microsoft.com...
> > Thanks for the info. I'm trying to return 17 rows of a table completely
> > intact to be used as the same table outside of the function. What i
> > notice
> > happening is that when it leaves the function, it still looks like
> > everything
> > is there, but i no longer have access to table.rows.count. It is
> > functioning
> > like an array instead of a datatable. Here's my function:
> > function load-table
> > {
> > [xml]$subnets = gc ~\Desktop\halls.xml
> > $table = New-Object system.Data.DataTable "subnets"
> > $col1 = New-Object system.data.DataColumn building
> > $col2 = New-Object system.data.DataColumn oc1, ([int])
> > $col3 = New-Object system.data.DataColumn oc2, ([int])
> > $col4 = New-Object system.data.DataColumn oc3, ([int])
> > $col5 = New-Object system.data.DataColumn oc4, ([int])
> > $col6 = New-Object system.data.DataColumn range
> > $table.columns.add($col1)
> > $table.columns.add($col2)
> > $table.columns.add($col3)
> > $table.columns.add($col4)
> > $table.columns.add($col5)
> > $table.columns.add($col6)
> > $i=0
> > $j= $subnets.subnets.subnet.length
> > $row = @()
> > while ($i -lt $j)
> > {
> > [array]$row += ""
> > $row[$i] = $table.newrow()
> > $row[$i].building = $subnets.subnets.subnet[$i].building
> > $row[$i].oc1 = $subnets.subnets.subnet[$i].octet1
> > $row[$i].oc2 = $subnets.subnets.subnet[$i].octet2
> > $row[$i].oc3 = $subnets.subnets.subnet[$i].octet3
> > $row[$i].oc4 = $subnets.subnets.subnet[$i].octet4
> > $row[$i].range = $subnets.subnets.subnet[$i].sub
> > $table.rows.add($row[$i])
> > $i++
> > }
> > Write-Host $table.rows.count
> > return $table
> > }
> >
> > $table
> > $table.rows.count
> >
> > $table.rows.count returns "17" inside the function, but nothing outside
> > the
> > function. In fact, though all the data is still there, $table.rows no
> > longer
> > exists.
> >
> > I hope that makes some sense.
> >
> > Thanks for your help.
> >
> > Janssen
> >
> >
> >
> > "Janssen" wrote:
> >
> >> Hello. I've written a routine that writes an XML file into a Data Table
> >> ([system.Data.DataTable]), and it performs exactly the way I want it to,
> >> but
> >> when i put the routine inside a function and try to return the table at
> >> the
> >> end by using "return $table", it loses the datatype of
> >> [system.Data.DataTable]. It becomes an array. I can verify this by
> >> running
> >> $table.gettype() before and after it exits the function. Before the
> >> return,
> >> it says System.Data.DataRow. After the return, it says System.Array.
> >> I've
> >> tried forcing it by saying [System.Data.Table]$table = loadtable but it
> >> errors with the following message:
> >> Cannot convert "System.Object[]" to "System.Data.DataTable".
> >> You cannot call a method on a null-valued expression.
> >>
> >> So really, my question is, in Powershell, can you tell a function to
> >> return
> >> it's data as a specific data type? Any help would be appreciated.
> >>
> >> Thanks!
> >>
> >> Janssen Jones
> >>

>
>
>

My System SpecsSystem Spec
Old 02-07-2007   #6 (permalink)
Janssen
Guest


 

Re: How to return a specific type from a function

That worked! Thanks a million. I've had trouble with "," and arrays before,
but I missed that this time....

Thanks again for such a fast response.

Janssen
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
select-string return type chang.luo@gmail.com PowerShell 3 01-12-2007 11:21 PM
Return type from function (Is it an egg or a chicken?) rockmoose PowerShell 3 12-21-2006 04:41 AM
return keyword in trap function MKielman PowerShell 0 10-11-2006 10:50 AM
Info: Can you Type a return from a function. Brandon Shell PowerShell 12 09-06-2006 11:50 PM
HOW TO: Return an Object from a Function/Filter Brandon Shell PowerShell 8 08-21-2006 12:16 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008