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 - why is a function level variable cleared in the Trap statement

Reply
 
Old 01-23-2009   #1 (permalink)
AdityaKir


 
 

why is a function level variable cleared in the Trap statement

Is there any reason why the object created within the trap is cleared once it
moves out of the trap. Below is an working sample code. In this $objArray
will not have the $output object created inside the trap. Once the function
is finished execution. Wanted to know why this happens and how to avoid it.

function Test
{
$arr = "Mangoes","Apples"
$insideArr = "ABC", "PQR"
$objArray = @()

foreach($a in $arr)
{
trap {continue}
&{trap{
write-Host "its here"
$output = New-Object object
Add-Member -inputobject $output -membertype NoteProperty -name
"Name" -value "here mangoes"
$objArray += $output
break}

if($a -eq "Mangoes")
{
throw "Error happening"
}
else
{
$output = new-Object Object
add-Member -inputObject $output -membertype NoteProperty
-name "Name" $a
$objArray += $output
}
write-Host $a}

}


return $objArray
}

return test

break

$objArray = test;

foreach($arrItem in $objArray)
{
$arrItem.Name
}

My System SpecsSystem Spec
Old 01-24-2009   #2 (permalink)
Shay Levy [MVP]


 
 

Re: why is a function level variable cleared in the Trap statement

Hi AdityaKir,


Variables created inside TRAP are local to the trap scope.

You can workaround it by prefixing $objArray calls with a scope name:

$scriptbjArray = @()

and

$scriptbjArray += $output



For more help:

PS > help about_scope



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



A> Is there any reason why the object created within the trap is cleared
A> once it moves out of the trap. Below is an working sample code. In
A> this $objArray will not have the $output object created inside the
A> trap. Once the function is finished execution. Wanted to know why
A> this happens and how to avoid it.
A>
A> function Test
A> {
A> $arr = "Mangoes","Apples"
A> $insideArr = "ABC", "PQR"
A> $objArray = @()
A> foreach($a in $arr)
A> {
A> trap {continue}
A> &{trap{
A> write-Host "its here"
A> $output = New-Object object
A> Add-Member -inputobject $output -membertype NoteProperty
A> -name
A> "Name" -value "here mangoes"
A> $objArray += $output
A> break}
A> if($a -eq "Mangoes")
A> {
A> throw "Error happening"
A> }
A> else
A> {
A> $output = new-Object Object
A> add-Member -inputObject $output -membertype
A> NoteProperty
A> -name "Name" $a
A> $objArray += $output
A> }
A> write-Host $a}
A> }
A>
A> return $objArray
A> }
A> return test
A>
A> break
A>
A> $objArray = test;
A>
A> foreach($arrItem in $objArray)
A> {
A> $arrItem.Name
A> }


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to get more information about the error from trap statement PowerShell
How to pass exceptions message outside the trap statement PowerShell
Specifying a trap statement's exception as a variable PowerShell
Multiple trap handlers in a function: variable scoping issues PowerShell
return keyword in trap function 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