首页 > 解决方案 > 通过函数在powershell中更改变量值的问题

问题描述

我在 Powershell 中使用表单编写了一个简单的井字游戏,我遇到了这个问题:

我声明了描述按钮内容的字符串变量和一个定义轮到的布尔变量。后来在函数中,我想更改布尔变量,但它仍然保持不变。我完全是新手,所以我想为我的问题获得最简单的解释。

 [string]$p1 = "n" 
 [string]$p2 = "n" 
 [string]$p3 = "n"
 [string]$p4 = "n"
 [string]$p5 = "n"
 [string]$p6 = "n"
 [string]$p7 = "n"
 [string]$p8 = "n"
 [string]$p9 = "n"
 $who=$true


 Function check1 ([string]$P){
 if ($P -eq "n"){
     if($who -eq $true){
         $Button[0,0].Image=[System.Drawing.Image]::FromFile("C:\Users\klima\Desktop\SYOP\kolko.bmp")
         $P="o"
         $who=$false
         }
     }
    else{
         $Button[0,0].Image=[System.Drawing.Image]::FromFile("C:\Users\klima\Desktop\SYOP\krzyzyk.bmp")
         $P="x"
         $who=$true
         }

 }


      Function check2 ($P){
      if ($P -eq "n"){
     if($who -eq $true){
         $Button[0,1].Image=[System.Drawing.Image]::FromFile("C:\Users\klima\Desktop\SYOP\kolko.bmp")
         $P="o"
         $who=$false
         }
     }
     else{
         $Button[0,1].Image=[System.Drawing.Image]::FromFile("C:\Users\klima\Desktop\SYOP\krzyzyk.bmp")
         $P="x"
         }
 }

稍后在代码中我向该按钮添加点击

$Button[0,o].Add_Click({check1($p1)}) $Wybor[0,1].Add_Click({check2($p2)}) 我的意思是我想更改布尔变量但它没有这样做,有人可以解释一下为什么吗?

标签: formsfunctionpowershellvariablesboolean

解决方案


推荐阅读