Reputation: 684
Before posting, I looked at PowerShell function doesn't produce output and Function not Returning Data both links didn't help.
I have a function named getState. When I call it, nothing is returned. When I run the debugger, I can see the var $state getting set with "foo" but the getter doesn't return the value of $state.
Here's the code:
$Global:state
function setState {
param(
[string]$s
)
$state = $s
}
function getState {
return $state
}
setState ("foo")
Write-host getState
How can I get the line Write-host getState to show foo? Thanks!
Upvotes: 0
Views: 386
Reputation: 1177
If you want to be sure the global var is used in the functions, specify it as global.
Try this:
$Global:state = $null
function setState {
param(
[string]$s
)
$Global:state = $s
}
function getState {
return $Global:state
}
setState ("foo")
getState
Upvotes: 2