Another newbie question...
I'm hung up on variable scope. Comparing to VbScript, I would like to have
global/public variables visible anywhere within the script, as well as
private/local variables visible only within their own function. As I
understand the docs, global or script scope should be visible anywhere
within a single script.
More specifically, I'm trying to update an global/public array within a
function. The array is visible to the function, and the function adds
members to the array, but on exiting the function, the array reverts to its
state prior to calling the function.
Obviously, I'm missing something here, and its not frustration...
Code sample follows. I can step through this and see the variables
changing. Just don't understand why I lose the modifications made in the
function.
##################################################################################
CLS
$Global:arrAccumulateG = @()
$arrAccumulateG += "Initial entry"
Set-Variable -Name strTest1G -Value 'Intitial value' -Scope Global
Write-Host "Global Variables"
Get-Variable -Scope Global -Name arr*,str*
###################################################################################
Function Accumulate( $StringP )
{
$arrAccumulateG += $StringP
$arrAccumulateG += "Something fixed..."
Get-Variable -Name arrAccumulateG
$strTest1G = $StringP
Write-Host " Added to array:" $arrAccumulateG[ -2..-1 ]
Write-Host " Set value strTest1G: $strTest1G`n"
}
####################################################################################These calls work as expected. (sort of)
#Value is passed in and added to existing, initial, array member.
#However...
# On return from Accumulate function, content of $arrAccumulateG reverts
# to its initial content.
#The initial content is seen in the Accumulate() function, but the
# modification, within the function is lost on return.
Accumulate( "This is line 1." )
Accumulate( "This is line 2." )
Accumulate( "This is line 3." )
Get-Variable -Name arrAccumulateG
|
|
0
|
|
|
|
Reply
|
TreyS
|
1/5/2010 4:18:24 PM |
|
Hi They,
in case of global variable, you must say specifically that you want to
modify global variable:
##################################################################################
CLS
$Global:arrAccumulateG = @()
$Global:arrAccumulateG += "Initial entry"
Set-Variable -Name strTest1G -Value 'Intitial value' -Scope Global
Write-Host "Global Variables"
Get-Variable -Scope Global -Name arr*,str*
###################################################################################
Function Accumulate( $StringP )
{
$Global:arrAccumulateG += $StringP
$Global:arrAccumulateG += "Something fixed..."
....
I think you got the idea
Martin
"TreyS" <TreyS@HotMail.com> wrote in message
news:158D8C41-7613-4C41-92FE-43A391FBCB9C@microsoft.com...
> Another newbie question...
>
> I'm hung up on variable scope. Comparing to VbScript, I would like to
> have global/public variables visible anywhere within the script, as well
> as private/local variables visible only within their own function. As I
> understand the docs, global or script scope should be visible anywhere
> within a single script.
>
> More specifically, I'm trying to update an global/public array within a
> function. The array is visible to the function, and the function adds
> members to the array, but on exiting the function, the array reverts to
> its state prior to calling the function.
>
> Obviously, I'm missing something here, and its not frustration...
>
> Code sample follows. I can step through this and see the variables
> changing. Just don't understand why I lose the modifications made in the
> function.
>
> ##################################################################################
> CLS
>
> $Global:arrAccumulateG = @()
> $arrAccumulateG += "Initial entry"
> Set-Variable -Name strTest1G -Value 'Intitial value' -Scope Global
>
> Write-Host "Global Variables"
> Get-Variable -Scope Global -Name arr*,str*
>
> ###################################################################################
> Function Accumulate( $StringP )
> {
> $arrAccumulateG += $StringP
> $arrAccumulateG += "Something fixed..."
> Get-Variable -Name arrAccumulateG
>
> $strTest1G = $StringP
> Write-Host " Added to array:" $arrAccumulateG[ -2..-1 ]
> Write-Host " Set value strTest1G: $strTest1G`n"
> }
> ####################################################################################These
> calls work as expected. (sort of)
> #Value is passed in and added to existing, initial, array member.
> #However...
> # On return from Accumulate function, content of $arrAccumulateG reverts
> # to its initial content.
> #The initial content is seen in the Accumulate() function, but the
> # modification, within the function is lost on return.
>
> Accumulate( "This is line 1." )
> Accumulate( "This is line 2." )
> Accumulate( "This is line 3." )
>
> Get-Variable -Name arrAccumulateG
>
>
|
|
0
|
|
|
|
Reply
|
Martin
|
1/5/2010 5:33:54 PM
|
|
|
1 Replies
744 Views
(page loaded in 0.044 seconds)
|