Reputation: 1910
How to create a powershell class that has static variables?
class ex1 {
static [int]$count = 0
ex1() {
[ex1]::count = [ex1]::count + 1
write-host [ex1]::count
}
}
$ex1 = [ex1]::new()
$ex2 = [ex1]::new()
$ex3 = [ex1]::new()
exit 1
I tried this but all it does it prints:
[ex1]::new()
[ex1]::new()
[ex1]::new()
instead of incrementing the count to count the number of objects created in the static integer.
Upvotes: 4
Views: 3926
Reputation: 27766
Simple solution: Enclose static variable references like [ex1]::count
in parentheses, whenever you want to pass them as an argument to another command like Write-Host
:
Write-Host ([ex1]::count)
Why is that necessary when simple variables like $someVar
don't require parentheses? For the same reason that Write-Host 2+2
doesn't print 4
but literally 2+2
: PowerShells often counterintuitive argument parsing mode.
PowerShell switches from normal expression parsing mode to argument parsing mode, whenever it sees a call to a command (compare with a call to a .NET function, which doesn't change parsing mode). This mode follows its own rules, e. g. dollar sign followed by variable name causes PS to resolve the variable - as expected. Surprisingly there isn't a rule that covers static variable references, so PowerShell falls back to interpreting the argument as literal string.
Enclosing [ex1]::count
in parentheses fixes the problem by forcing the parser to leave argument mode and parse an expression.
Full code sample with fix:
class ex1 {
static [int]$count = 0
ex1() {
[ex1]::count = [ex1]::count + 1
# Argument parsing mode -> wrap static variable reference in parentheses
write-host ([ex1]::count)
}
}
$ex1 = [ex1]::new()
$ex2 = [ex1]::new()
$ex3 = [ex1]::new()
[ex1]::count # Expression parsing mode -> no parentheses needed!
Note: The added last line is an example for PowerShells implicit output feature. By just naming a variable on its own line, PowerShell writes its value to standard output. As there is no command invocation involved, PowerShell is still in expression parsing mode, so we don't need to bracketize the expression.
Upvotes: 8
Reputation: 61148
Apart from the constructor ex1() {..}
, you need to add a method that actually returns the value of the static property Count
:
class ex1 {
[int]static $count = 0
ex1() {
# constructor increments the static Count property
[ex1]::count++
}
[int]GetCount() {
# simply return the current value of Count
return [ex1]::count
}
}
$ex1 = [ex1]::new().GetCount()
$ex2 = [ex1]::new().GetCount()
$ex3 = [ex1]::new().GetCount()
$ex1, $ex2, $ex3 # --> resp. 1, 2, 3
Upvotes: 5