flags enum are cool

This topic contains 1 reply, has 2 voices, and was last updated by  Robert Thomson 2 months, 3 weeks ago.

  • Author
  • #94831


    Wow, I never knew about this easy way to deal with bitmasks.

    [flags()] Enum ErrorTable
      Error1 = 0x01
      Error2 = 0x02
      Error3 = 0x04
      Error4 = 0x08
      Error5 = 0x10
      Error6 = 0x20
    PS C:\> $errorcodes = [ErrorTable]'Error1,Error2'
    PS C:\> $errorcodes
    Error1, Error2
    PS C:\> $errorcodes = [ErrorTable]3
    PS C:\> $errorcodes
    Error1, Error2
    PS C:\> $errorcodes.HasFlag([ErrorTable]'Error2')                                          
  • #95029

    Robert Thomson

    It is indeed a cool feature but I believe the enum keyword only became available in Powershell 5. If you are stuck in the functional abyss of powershell 4 or less then it's still back to inlinining c# code.

You must be logged in to reply to this topic.