I'm defining my first custom class in powershell 5.1. I'm creating methods for my class. MS Learn tells me I must have a return type in the method definition but it constantly generates an error in the IDE
# Define a class
class MyError
{
[string] $date
[ErrorRecord] $PSErrorObject
[string] $eMail
[string] $Script
[string] $scriptFolder
[int] $LineNumber
[string] $PSErrorMessage
[ValidateSet("Administrator","User")] [string] $Privilege
[string] $ScriptVersion
[string] $LogFile
[string] $LogFileFolder
[string] $WorkstationName
[string] $UserName
# Process the error. Main control method. Calls the appropriate methods. Called when an
# error is caught.
[string] Process() {
return "foobar"
}
}
The '[string] ' Process() line is red underlined saying: Missing closing ']' in Statement block or type definition
If I remove the [string] it's fine but violates what MS says is required for a method in a class definition. Also, if I just copy & paste their example (which looks the same) it's also happy.
# Method
[string] GetFullName() {
return "$($this.FirstName) $($this.LastName)"
}
I'm most confused.
# Define a class
class MyError
{
[string] $date
[ErrorRecord] $PSErrorObject
[string] $eMail
[string] $Script
[string] $scriptFolder
[int] $LineNumber
[string] $PSErrorMessage
[ValidateSet("Administrator","User")] [string] $Privilege
[string] $ScriptVersion
[string] $LogFile
[string] $LogFileFolder
[string] $WorkstationName
[string] $UserName
# Process the error. Main control method. Calls the appropriate methods. Called when an
# error is caught.
[string] Process() {
return "foobar"
}
}
The '[string] ' Process() line is red underlined saying: Missing closing ']' in Statement block or type definition
If I remove the [string] it's fine but violates what MS says is required for a method in a class definition. Also, if I just copy & paste their example (which looks the same) it's also happy.
# Method
[string] GetFullName() {
return "$($this.FirstName) $($this.LastName)"
}
I'm most confused.