3

I'm trying to define a class in Powershell v5 and I can't access variables from within class functions.

Ex.

PS C:\> class Foo{ $bar = 'foobar' mymethod(){ $bar + '123' } } PS C:\> [Foo]::new().mymethod() PS C:\> At line:4 char:11 + $bar + '123' Variable is not assigned in the method. 

1 Answer 1

8

Use $this to access your variable:

class Foo{ $bar = 'foobar' [string] mymethod(){ return $this.bar + '123' } } 

Output:

foobar123 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.