flags enum are cool

Welcome Forums General PowerShell Q&A flags enum are cool

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
8 months, 2 weeks ago.

  • Author
    Posts
  • #94831
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    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')                                          
    True
    
  • #95029

    Participant
    Points: 22
    Rank: Member

    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.

The topic ‘flags enum are cool’ is closed to new replies.