Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell: How to create and access static variables in a powershell class?

Tags:

powershell

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.

like image 920
pico Avatar asked Oct 17 '25 11:10

pico


2 Answers

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.

like image 76
zett42 Avatar answered Oct 20 '25 15:10

zett42


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
like image 20
Theo Avatar answered Oct 20 '25 16:10

Theo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!