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 Tutorial - Multiple trap handlers in a function: variable scoping issues

Reply
 
Old 10-13-2006   #1 (permalink)
Andrew Webb
Guest


 
 

Multiple trap handlers in a function: variable scoping issues


I'm trying to write the PowerShell equivalent of this:-


void MyMethod ()
{
FileInfo fi = null;
try
{
fi = new FileInfo (@"C:\out.txt");
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
return;
}


FileInfo fi1 = null;
try
{
fi1 = new FileInfo (@"C:\out1.txt");
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
return;
}


// use 'fi' and 'f1'; e.g. read their Exists properties
MessageBox.Show (fi.Exists.ToString ());
MessageBox.Show (fi1.Exists.ToString ());
}


-------------------------------------------------------

So far I have this:-


function Main
{
new-variable fi -value $null -option "AllScope"
&{
trap
{
write-host "Exception handler 1" + $_.Exception.Message
continue
}
.{
$fi = new-object -typename System.IO.FileInfo -argumentlist "C:\out.txt"
}
}
if ($fi -eq $null)
{
return
}


new-variable fi1 -value $null -option "AllScope"
&{
trap
{
write-host "Exception handler 2" + $_.Exception.Message
continue
}
.{
$fi1 = new-object -typename System.IO.FileInfo -argumentlist
"C:\out1.txt"
}
}
if ($fi1 -eq $null)
{
return
}


write-host $fi.Exists
write-host $fi1.Exists
}

.. Main

write-host $fi.Exists
write-host $fi1.Exists


-------------------------------------------------------

I.e.
- use script blocks in order to have multiple trap handlers in the same
function
- use the new-variable cmdlet with the "AllScope" option to ensure that
'fi' and
'fi1' exist at function scope and script block scope

Unfortunately, "AllScope" means just that. Hence the two write-host
statements after
the call to Main do work, but ideally they should not.

Am I on the right track? Is there a way to keep the two variables at
function scope?
Is there a fundamentally better/different way than my approach?

TIA

Andrew



My System SpecsSystem Spec
Old 10-13-2006   #2 (permalink)
Andrew Webb
Guest


 
 

RE: Multiple trap handlers in a function: variable scoping issues

Er, I think I solved it. I change this:-
. Main
to this:-
& Main

Then variables created by the function are at function scope.

I'd still be interested to know if I've taken the right approach to having
multiple trap handlers in a function. Thanks!

Andrew

My System SpecsSystem Spec
Old 10-13-2006   #3 (permalink)
Andrew Webb
Guest


 
 

RE: Multiple trap handlers in a function: variable scoping issues

OTOH... the following works, without having to use 'new-variable'. Sometimes
dot-sourcing is the right thing to do, and at other times it's not. You
really have to know. I'm getting there...

function Main
{
.{
trap
{
write-host "Exception handler 1" + $_.Exception.Message
continue
}
.{
$fi = new-object -typename System.IO.FileInfo -argumentlist "C:\out.txt"
}
}
if ($fi -eq $null)
{
return
}


.{
trap
{
write-host "Exception handler 2" + $_.Exception.Message
continue
}
.{
$fi1 = new-object -typename System.IO.FileInfo -argumentlist
"C:\out1.txt"
}
}
if ($fi1 -eq $null)
{
return
}


write-host $fi.Exists
write-host $fi1.Exists

}

Main # the same as doing: & Main

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
why is a function level variable cleared in the Trap statement PowerShell
Using a variable from within a function PowerShell
Specifying a trap statement's exception as a variable PowerShell
Set a variable in a function then pass it off? 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