Function Grief!!!

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 6 years and 2 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
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Function Grief!!!

Post by jsira2003@yahoo.com »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build:
32 or 64 bit version of product:
Operating system:
32 or 64 bit OS:

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

I am using the latest version of powershell studio on windows 10 pro. I am having trouble with a function returning values. I know the return command is problematic. The following code below works fine in powershell_ise.exe. If I place the function in the globals of my project and put the call in the main script when somebody presses a button the values do not get returned to $p. This is perplexing! Return does not work. I tried write-host and that did not work. Now I tried the suggested Microsoft method and this didn't work. My only other possibility is to use global variables for p1 - p120. I am trying to avoid this. Do you have any ideas why this will not work in powershell studio yet it works just fine in powershell_ise.exe?

thanks,
John

Function Positions([string]$program, [string]$variable)
{
#assignments
$p1 = 5
$p2 = 10
$p3 = 15
$p4 = 20
$p5 = 25
$p6 = 30
$p7 = 35
$p8 = 40
$p9 = 45
$p10 = 50
$p11 = 55
$p12 = 60
$p13 = 65
$p14 = 70
$p15 = 75
$p16 = 80
$p17 = 85
$p18 = 90
$p19 = 95
$p20 = 100
$p21 = 105
$p22 = 110
$p23 = 115
$p24 = 120
$p25 = 125
$p26 = 130
$p27 = 135
$p28 = 140
$p29 = 145
$p30 = 150
$p31 = 155
$p32 = 160
$p33 = 165
$p34 = 170
$p35 = 175
$p36 = 180
$p37 = 185
$p38 = 190
$p39 = 195
$p40 = 200

#returned values
$p1; $p2; $p3; $p4; $p5; $p6; $p7; $p8; $p9; $p10;
$p11; $p12; $p13; $p14; $p15; $p16; $p17; $p18; $p19; $p20;
$p21; $p22; $p23; $p24; $p25; $p26; $p27; $p28; $p29; $p30;
$p31; $p32; $p33; $p34; $p35; $p36; $p37; $p38; $p39; $p40;
$p41; $p42; $p43; $p44; $p45; $p46; $p47; $p48; $p49; $p50;
$p51; $p52; $p53; $p54; $p55; $p56; $p57; $p58; $p59; $p60;
$p61; $p62; $p63; $p64; $p65; $p66; $p67; $p68; $p69; $p70;
$p71; $p72; $p73; $p74; $p75; $p76; $p77; $p78; $p79; $p80;
$p81; $p82; $p83; $p84; $p85; $p86; $p87; $p88; $p89; $p90;
$p91; $p92; $p93; $p94; $p95; $p96; $p97; $p98; $p99; $p100;
$p101; $p102; $p103; $p104; $p105; $p106; $p107; $p108; $p109; $p110;
$p111; $p112; $p113; $p114; $p115; $p116; $p117; $p118; $p119; $p120;
return
}
cls
$program = ""
$variable = ""
#the call in the main script...
$p = Positions $Program $variable
for($i=0; $i -lt $p.length; $i++)
{
$variable = 'p' + [string]($i+1)
Set-Variable -Name $variable -Value $p[$i]

}

for($i=0; $i -lt $p.length; $i++)
{
get-variable -name ('p'+[string]($i+1))

}
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Function Grief!!!

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

You running into a scoping issue. Try specifying the script scope:

Code: Select all

$script:p1 = 5

https://www.sapien.com/blog/2015/09/09/ ... l-gui-app/
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Function Grief!!!

Post by jvierra »

First of all, what is the point of this. It seems like a lot of code to get a list of numbers,

The following are all equivalent:

Code: Select all

Function Positions([string]$program, [string]$variable) {
	#assignments
	$p1 = 5
	$p2 = 10
	$p3 = 15
	$p4 = 20
	$p5 = 25
	$p6 = 30
	$p7 = 35
	$p8 = 40
	$p9 = 45
	$p10 = 50
	$p11 = 55
	$p12 = 60
	$p13 = 65
	$p14 = 70
	$p15 = 75
	$p16 = 80
	$p17 = 85
	$p18 = 90
	$p19 = 95
	$p20 = 100
	$p21 = 105
	$p22 = 110
	$p23 = 115
	$p24 = 120
	$p25 = 125
	$p26 = 130
	$p27 = 135
	$p28 = 140
	$p29 = 145
	$p30 = 150
	$p31 = 155
	$p32 = 160
	$p33 = 165
	$p34 = 170
	$p35 = 175
	$p36 = 180
	$p37 = 185
	$p38 = 190
	$p39 = 195
	$p40 = 200
	
	#returned values
	$p1; $p2; $p3; $p4; $p5; $p6; $p7; $p8; $p9; $p10;
	$p11; $p12; $p13; $p14; $p15; $p16; $p17; $p18; $p19; $p20;
	$p21; $p22; $p23; $p24; $p25; $p26; $p27; $p28; $p29; $p30;
	$p31; $p32; $p33; $p34; $p35; $p36; $p37; $p38; $p39; $p40;
	$p41; $p42; $p43; $p44; $p45; $p46; $p47; $p48; $p49; $p50;
	$p51; $p52; $p53; $p54; $p55; $p56; $p57; $p58; $p59; $p60;
	$p61; $p62; $p63; $p64; $p65; $p66; $p67; $p68; $p69; $p70;
	$p71; $p72; $p73; $p74; $p75; $p76; $p77; $p78; $p79; $p80;
	$p81; $p82; $p83; $p84; $p85; $p86; $p87; $p88; $p89; $p90;
	$p91; $p92; $p93; $p94; $p95; $p96; $p97; $p98; $p99; $p100;
	$p101; $p102; $p103; $p104; $p105; $p106; $p107; $p108; $p109; $p110;
	$p111; $p112; $p113; $p114; $p115; $p116; $p117; $p118; $p119; $p120;
	return
}
$p = Positions
$p

Code: Select all

Function Positions([string]$program, [string]$variable) {
	#assignments
	5.10,15,20,25,30,35 # etc
}
$p = Positions
$p

Code: Select all

Function Positions([string]$program, [string]$variable) {
	1..40 |%{$_ * 5}
}
$p = Positions
$p

Code: Select all

$p = 	1..40 |%{$_ * 5}
$P
1 .. 40 | %{$_ * 5} | %{New-Variable -Name "p$_" -Value $_ -Scope Script}
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: Function Grief!!!

Post by jsira2003@yahoo.com »

I changed the numbers and didn't state the goal of the function since it is security related. It sounds like the scope is my answer is that correct? If I need visibility outside the function itself you set the option to script. If I am understanding this correctly I'll give it a try.

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

Re: Function Grief!!!

Post by jvierra »

There is no way to advise you when your question is this vague.

Basically I see no good technical reason for what you are trying to do. Security has nothing to do with code. Security is a system issue. It must predate coding constructs. You cannot and shoul never rely on code obfuscation as a security measure. It will only lead to security mistakes.
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: Function Grief!!!

Post by jsira2003@yahoo.com »

lol! Actually that was the answer $script:p1 on each of my variables in the function that allowed the values to be returned to the $p array by the calling function. I didn't go into the details of my application or what I am trying to accomplish. I realize the application may not make sense to you. I just sent you enough so you could assist me with the function return values issue. I made up the values defined to the variables for illustration purposes only.

The only thing I wonder at this point is why did I need to change the scope to script to get those values to return. I was able to return those values without $script: in the example code I sent you in the powershell_ise environment. I could not achieve the same results in the powershell environment and had to script scope my variables. I suspect there is something in regards to a call to a function in global being returned to the main script where the values were not getting returned.

I still want to understand this. I appreciate your help. I already fixed my code and can continue my project.
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: Function Grief!!!

Post by jsira2003@yahoo.com »

I said powershell environment. I mean powershell studio environment.

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

Re: Function Grief!!!

Post by jvierra »

You can only change variables in an outer scope by specifying the scope.

See:
help about_scopes

To create a scoped variable just do this:
$global:myvar = 'stuff'

To read the variable anywhere just reference the variable:
$myvar

If you want to change the value of a global from a child scope you must use $global:myvar = 'newvalue'

If you scope to a script then those variables will disappear once that script ends.

In coding in any language or system it is critical that you fully understand how "scope" works.
This topic is 6 years and 2 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