Skip to main content
deleted 1 character in body
Source Link
zett42
  • 28.3k
  • 3
  • 44
  • 97

Simple solution: Enclose static variable references like [ex1]::count in parentheses, whenever you want to pass them as an argument to another functioncommand 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.

Simple solution: Enclose static variable references like [ex1]::count in parentheses, whenever you want to pass them as an argument to another function 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.

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.

Expanded on the expression vs. argument parsing modes
Source Link
zett42
  • 28.3k
  • 3
  • 44
  • 97

TheSimple solution: Enclose static property worksvariable references like [ex1]::count in parentheses, but surprisinglywhenever you want to pass them as an argument to another function like Write-Host:

Write-Host ([ex1]::count) 

Why is that necessary when simple variables like $someVar don't require parentheses? For the PowerShell parser does not leavesame reason that Write-Host 2+2 doesn't print 4 but literally 2+2: PowerShells often counterintuitive argument parsing mode when encountering.

PowerShell switches from normal [ex1]::count asexpression parsing mode to argument parsing mode, whenever it sees a call to a command (compare with a call to a .NET function argument, it is parsedwhich 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   write-host# ([ex1]::count)Argument parsing #mode <--> Fixwrap bystatic addingvariable reference in parentheses write-host ([ex1]::count) } } $ex1 = [ex1]::new() $ex2 = [ex1]::new() $ex3 = [ex1]::new() [ex1]::count # Works without parentheses, asExpression weparsing aremode not-> inno argumentparentheses mode.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. In this caseAs there is no command invocation involved, PowerShell is alreadystill in expression parsingexpression parsing mode, so we don't need to bracketize the expression.

The static property works, but surprisingly the PowerShell parser does not leave argument mode when encountering [ex1]::count as a function argument, it is parsed as a literal string.

Enclosing [ex1]::count in parentheses fixes the problem by forcing the parser to leave argument mode and parse an expression.

class ex1 { static [int]$count = 0 ex1() { [ex1]::count = [ex1]::count + 1 write-host ([ex1]::count) # <-- Fix by adding parentheses } } $ex1 = [ex1]::new() $ex2 = [ex1]::new() $ex3 = [ex1]::new() [ex1]::count # Works without parentheses, as we are not in argument mode. 

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. In this case PowerShell is already in expression parsing mode, so we don't need to bracketize the expression.

Simple solution: Enclose static variable references like [ex1]::count in parentheses, whenever you want to pass them as an argument to another function 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.

added 322 characters in body
Source Link
zett42
  • 28.3k
  • 3
  • 44
  • 97

The static property works, but surprisingly the PowerShell parser does not leave argument mode when encountering [ex1]::count as a function argument, it is parsed as a literal string.

Enclosing [ex1]::count in parentheses fixes the problem by forcing the parser to leave argument mode and parse an expression.

class ex1 { static [int]$count = 0 ex1() { [ex1]::count = [ex1]::count + 1 write-host ([ex1]::count) # <-- Fix by adding parentheses } } $ex1 = [ex1]::new() $ex2 = [ex1]::new() $ex3 = [ex1]::new() [ex1]::count # Works without parentheses, as we are not in argument mode. 

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. In this case PowerShell is already in expression parsing mode, so we don't need to bracketize the expression.

The static property works, but surprisingly the PowerShell parser does not leave argument mode when encountering [ex1]::count as a function argument, it is parsed as a literal string.

Enclosing [ex1]::count in parentheses fixes the problem by forcing the parser to leave argument mode and parse an expression.

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() [ex1]::count # Works without parentheses, as we are not in argument mode. 

The static property works, but surprisingly the PowerShell parser does not leave argument mode when encountering [ex1]::count as a function argument, it is parsed as a literal string.

Enclosing [ex1]::count in parentheses fixes the problem by forcing the parser to leave argument mode and parse an expression.

class ex1 { static [int]$count = 0 ex1() { [ex1]::count = [ex1]::count + 1 write-host ([ex1]::count) # <-- Fix by adding parentheses } } $ex1 = [ex1]::new() $ex2 = [ex1]::new() $ex3 = [ex1]::new() [ex1]::count # Works without parentheses, as we are not in argument mode. 

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. In this case PowerShell is already in expression parsing mode, so we don't need to bracketize the expression.

edited body
Source Link
mklement0
  • 452.5k
  • 68
  • 728
  • 988
Loading
Source Link
zett42
  • 28.3k
  • 3
  • 44
  • 97
Loading