Why is a variable defined locally successfully acc

2019-06-03 05:37发布

问题:

See code below. Put both files in the same directory and run Form1.ps1 from the PS ISE

As you can see, the (local) variable $localVar is defined in the event handler $button2_Click. As such, I assumed $localVar would not/could not exist outside the scope of $button2_Click with scope defined by the braces that define the event handler.

However, as you can see, I use the contents of $localVar to load $textbox2.Text in the function fA. When you click the Test button, both textboxes display the contents of $localVar

What's going on? Why is $button2_Click's $localVar accessible from within fA?

Form1.ps1

function fA
{
    $textbox2.Text = $localVar
}

$button2_Click = 
{
    $localVar = "set in `$button2_Click"
    $textbox1.Text = $localVar
    fA
}

. (Join-Path $PSScriptRoot 'Form1.designer.ps1')

$textbox1.Text = ""
$Form1.ShowDialog()

Form1.designer.ps1

[void][System.Reflection.Assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][System.Reflection.Assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
$Form1 = New-Object -TypeName System.Windows.Forms.Form
[System.Windows.Forms.Button]$button2 = $null
[System.Windows.Forms.TextBox]$textBox1 = $null
[System.Windows.Forms.TextBox]$textBox2 = $null
[System.Windows.Forms.Label]$label1 = $null
[System.Windows.Forms.Label]$label2 = $null
[System.Windows.Forms.Button]$button1 = $null
function InitializeComponent
{
$button2 = (New-Object -TypeName System.Windows.Forms.Button)
$textBox1 = (New-Object -TypeName System.Windows.Forms.TextBox)
$textBox2 = (New-Object -TypeName System.Windows.Forms.TextBox)
$label1 = (New-Object -TypeName System.Windows.Forms.Label)
$label2 = (New-Object -TypeName System.Windows.Forms.Label)
$Form1.SuspendLayout()
#
#button2
#
$button2.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]148,[System.Int32]12))
$button2.Name = [System.String]'button2'
$button2.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]77,[System.Int32]36))
$button2.TabIndex = [System.Int32]0
$button2.Text = [System.String]'Test'
$button2.UseVisualStyleBackColor = $true
$button2.add_Click($button2_Click)
#
#textBox1
#
$textBox1.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]67,[System.Int32]69))
$textBox1.Name = [System.String]'textBox1'
$textBox1.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]106,[System.Int32]20))
$textBox1.TabIndex = [System.Int32]1
#
#textBox2
#
$textBox2.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]247,[System.Int32]69))
$textBox2.Name = [System.String]'textBox2'
$textBox2.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]106,[System.Int32]20))
$textBox2.TabIndex = [System.Int32]2
#
#label1
#
$label1.AutoSize = $true
$label1.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]72))
$label1.Name = [System.String]'label1'
$label1.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]47,[System.Int32]13))
$label1.TabIndex = [System.Int32]3
$label1.Text = [System.String]'textbox1'
#
#label2
#
$label2.AutoSize = $true
$label2.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]194,[System.Int32]72))
$label2.Name = [System.String]'label2'
$label2.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]47,[System.Int32]13))
$label2.TabIndex = [System.Int32]4
$label2.Text = [System.String]'textbox2'
#
#Form1
#
$Form1.ClientSize = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]374,[System.Int32]110))
$Form1.Controls.Add($label2)
$Form1.Controls.Add($label1)
$Form1.Controls.Add($textBox2)
$Form1.Controls.Add($textBox1)
$Form1.Controls.Add($button2)
$Form1.Name = [System.String]'Form1'
$Form1.ResumeLayout($false)
$Form1.PerformLayout()
Add-Member -InputObject $Form1 -Name base -Value $base -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name button2 -Value $button2 -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name textBox1 -Value $textBox1 -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name textBox2 -Value $textBox2 -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name label1 -Value $label1 -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name label2 -Value $label2 -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name button1 -Value $button1 -MemberType NoteProperty
}
. InitializeComponent

回答1:

Function fA sees your variable, because it runs in a child scope of the script block in which $localVar was created - this is general PowerShell behavior, and not specific to the ISE.

When you create a variable with $localVar = ..., it is local in the following sense:

  • visible and directly modifiable in the same scope, but not in any parent scopes.

  • visible in all child scopes, but not directly modifiable there.

    • You can use the $private: scope modifier to prevent child scopes from seeing a variable.
    • If you assign to a variable (by name only) that was originally created in a parent scope, (e.g., $localVar = ...), you'll instead create a new, local variable, in the current scope, which shadows the original variable.
    • It is possible to modify a parent scope's variables, but you need to use either Set-Variable -Scope or a scope modifier such as $script: (see links below).

For more information, see:

  • Get-Help about_Scopes

  • the last section of this answer, which provides a concise summary.



回答2:

Hello So in ISE you have Powershell Tabs and Script Tabs.

A Powershell Tab is like running a single Powershell console. Each Script tab inside that powershell tab is using that console. So if in one script you define

$Hello = "TEST"

then run that in ise. Then in another script tab in the same powershell tab you run

$Hello

the output would be "TEST"

This is called Scope. Basically what variables code can see. All variable created in a powershell instance are always accessible in the rest of that instance

So lets get a little deeper and go over you example

function fA
{
    $textbox2.Text = $localVar
}

$button2_Click = 
{
    $localVar = "123"
    $textbox1.Text = $localVar
    fA
}

$localVar is created on click for the first time. The scope will carry on from that to anything created after it. So FA is called and $localvar is carried to FA scope. Textbox1 and 2 show 123. But lets show how there is still scope to this and its not global.

function TB1
{
    $localVar = "456"
    $textbox1.Text = $localVar # Will output 456
}

function TB2{
    $textbox2.Text = $localVar # Will output 123
}
$button2_Click = 
{
    $localVar = "123"
    TB1
    TB2
    fA
}

$localVar created for first time. Passed to the first function $localvar is still 123 but in the function its changed to 456. Once it leaves there it goes to second function where its $Localvar will equal 123. The variable changed in the function did not effect the variable that was in its parent aka the caller button2_click