0

Im writing a simple tic tac toe using forms in Powershell and i have this problem:

I declared string variables that describe the content of buttons and one boolean variable that define what turn is it. Later in function that i i wanted to change the boolean variable but it stills remain the same. I'm total newbie so i would like to get the simplest explenation for my problem possible.

 [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"
         }
 }

Later in the code im adding clicks to that buttons

$Button[0,o].Add_Click({check1($p1)}) $Wybor[0,1].Add_Click({check2($p2)}) My point is i want to change the boolean variable but it doesnt do so, can someone explain me why?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Kuba
  • 1
  • 1
  • 1
    please read `Get-Help about_Scopes` for the why of it ... [*grin*] – Lee_Dailey Apr 06 '20 at 16:58
  • I hope the [linked answer](https://stackoverflow.com/a/57649395/45375) solves the problem. In short: to change `$who` in the _script_ scope, use `$script:who = ...` – mklement0 Apr 06 '20 at 16:59

0 Answers0