Counting issue with variables

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
JorisK1980
Posts: 30
Last visit: Fri Nov 16, 2018 1:40 am

Counting issue with variables

Post by JorisK1980 »

Product, version and build: 4.2.99
32 or 64 bit version of product: 64
Operating system: W2012R2
32 or 64 bit OS: 64
PowerShell Version: 4

Hi,

1) I have in globals $i = 1
2) I have button1 that does $i++
3) I have button2 that does write-host $i

When i press Button 1 several times, i would assume that $i have a higher value then 1, but when i hit button 2, it outputs "1".

I have this issue now with a script that used to work fine for years, and now all of a sudden i have this issue.

Anyone any clue? Something simple like this should be working imho...
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Counting issue with variables

Post by DevinL »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.
User avatar
skalizar
Posts: 18
Last visit: Wed Nov 22, 2023 12:43 pm

Re: Counting issue with variables

Post by skalizar »

Most likely a scope issue. You've declared a global variable, but the $i you are incrementing in the button event is local.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Counting issue with variables

Post by jvierra »

$global:i++

When a variable is written to in an inner scope a new local variable is created. To avoid this specify the full scope of the variable.

see - help about_scopes

Global variables should be avoided except in very specific cases.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Counting issue with variables

Post by DevinL »

Another option could be to use the $script:i++ scope since the form is a single script file it shouldn't be necessary to use the global scope.

That being said, I'm making the assumption that when you say "1) I have in globals $i = 1" you really mean in your globals.ps1 file you have $i = 1 and you're referencing it from a form within your project.

If you're actually assigning $i to the global scope, then you'll have to do what Jvierra said with $global:i++.
DevinL
SAPIEN Technologies, Inc.
User avatar
JorisK1980
Posts: 30
Last visit: Fri Nov 16, 2018 1:40 am

Re: Counting issue with variables

Post by JorisK1980 »

Thanks a lot guys, it seems to work.

I have always worked with simply $i++, i don't have a problem to now use $script:i++ but can anyone explain to me why my old way of defining $i all of a sudden doesn't work anymore? This script has worked for years.

Anyways, many thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Counting issue with variables

Post by jvierra »

When in a form you are in a sub-scope. "global" is in the global scope. You cannot directly change a variable in an outer scope without referencing the scope it is contained in.

See help about_scopes for a complete explanation.

This could never have worked for years as PowerShell is not designed that way. THIs is how all nearly programming languages work. It is fundamental.
User avatar
jazz albert
Posts: 16
Last visit: Sat Nov 05, 2016 3:09 pm

Re: Counting issue with variables

Post by jazz albert »

I think there must some way to get it done!
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked