代码之家  ›  专栏  ›  技术社区  ›  VA systems engineer

为什么本地定义的变量可以从另一个函数中成功访问?

  •  1
  • VA systems engineer  · 技术社区  · 6 年前

    Form1.ps1 从PS ISE

    如您所见,(局部)变量 $localVar 在事件处理程序中定义 $button2_Click 不会/不可能存在于 $button2\点击 范围由定义事件处理程序的大括号定义。

    但是,如您所见,我使用 $本地变量 加载 $textbox2.Text 在函数中 fA . 当您单击 Test $本地变量

    $本地变量 可从内部访问 fA公司 ?

    表1.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()
    

    [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
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   mklement0    6 年前

    fA 看到你的变量,因为它运行在 小孩 脚本块的作用域,其中 $localVar 已创建-这是一般的PowerShell行为,而不是特定于ISE。

    创造 $localVar = ... 是的 在以下意义上:

    • 可见且可直接修改 在同一范围内,但不在任何范围内 起源

    • 看得见的 在里面 所有孩子 范围,但不能直接修改。

      • 你可以用 $private: 作用域修饰符,防止子作用域看到变量。
      • 如果指定给最初在中创建的变量(仅按名称) 范围(例如。, ),您将改为创建 新的 ,局部变量,在 现在的
      • 可以修改父作用域的变量,但您需要使用 Set-Variable -Scope $script: (参见下面的链接)。

    有关详细信息,请参阅:

        2
  •  0
  •   ArcSet    6 年前

    你好,所以在ISE中你有Powershell选项卡和脚本选项卡。

    $Hello = "TEST"
    

    然后在ise中运行。然后在运行的同一powershell选项卡中的另一个脚本选项卡中

    $你好

    输出将是“TEST”

    这称为范围。基本上代码可以看到什么变量。 在powershell实例中创建的所有变量在该实例的其余部分中总是可以访问的

    让我们再深入一点,看看你的例子

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

    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
    }
    

    $ 第一次创建。传递给第一个函数$ 局部变量 仍然是 123 但在功能上它变成了 456 . 一旦它离开那里,它就进入第二个函数$ 123 . 函数中更改的变量不影响其父函数(即调用者)中的变量

    推荐文章