locked
Brackets Around [Enum] In Looping Thru Enumeration RRS feed

  • Question

  • User2023521679 posted

    I wrote the following code to print a literal string for the Active Directory userAccountControl attribute. It works pretty well. My question is, why are the square brackets ncessary around the Enum in my code sample. What do they do and why are they required?

     

    Public Enum AccountControlComponents As Integer
          SCRIPT = &H1
          ACCOUNTDISABLE = &H2
          HOMEDIR_REQUIRED = &H8
          LOCKOUT = &H10
          PASSWD_NOTREQD = &H20
          PASSWD_CANT_CHANGE = &H40
          ENCRYPTED_TEXT_PWD_ALLOWED = &H80
          TEMP_DUPLICATE_ACCOUNT = &H100
          NORMAL_ACCOUNT = &H200
          INTERDOMAIN_TRUST_ACCOUNT = &H800
          WORKSTATION_TRUST_ACCOUNT = &H1000
          SERVER_TRUST_ACCOUNT = &H2000
          DONT_EXPIRE_PASSWORD = &H10000
          MNS_LOGON_ACCOUNT = &H20000
          SMARTCARD_REQUIRED = &H40000
          TRUSTED_FOR_DELEGATION = &H80000
          NOT_DELEGATED = &H100000
          USE_DES_KEY_ONLY = &H200000
          DONT_REQ_PREAUTH = &H400000
          PASSWORD_EXPIRED = &H800000
          TRUSTED_TO_AUTH_FOR_DELEGATION = &H1000000
          PARTIAL_SECRETS_ACCOUNT = &H400000
       End Enum
       
       Function DecodeUserAccountControl(ByVal userAccountControl As Integer) As String
          Dim r As String = ""
    
          For Each AccountControlComponent As Integer In [Enum].GetValues(GetType(AccountControlComponents))
             If CBool(userAccountControl And AccountControlComponent) Then
    
                If r = "" Then
                   r = String.Format("0x{0:x} = (", userAccountControl)
                Else
                   r &= "|"
                End If
    
                r &= [Enum].GetName(GetType(AccountControlComponents), AccountControlComponent)
             End If
          Next
    
          r = r & ")"
    
          Return r
       End Function

    Friday, January 10, 2014 10:36 AM

Answers

  • User-760709272 posted

    Seems it's a vb.net thing.

    Enum is used to mark something as being an enum (see your Public Enum AccountControlComponents for an example).  When you use Enum in your function, you are not saying you want to define an Enum (via the Enum keyword) you are saying you want to use the Enum type (System.Enum).  Putting square brackets lets vb.net know you are not intending the thing in the brackets to be a keyword but a variable, type etc.  if you wanted the code to work without them, try replacing [Enum] with System.Enum

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2014 10:56 AM

All replies

  • User-760709272 posted

    Putting square brackets around things allows there to be spaces in them.  There are no spaces in Enum so you don't need them.

    Friday, January 10, 2014 10:41 AM
  • User2023521679 posted

    My code will not compile without them. I get the error Expression Expected.

    Friday, January 10, 2014 10:44 AM
  • User-760709272 posted

    Seems it's a vb.net thing.

    Enum is used to mark something as being an enum (see your Public Enum AccountControlComponents for an example).  When you use Enum in your function, you are not saying you want to define an Enum (via the Enum keyword) you are saying you want to use the Enum type (System.Enum).  Putting square brackets lets vb.net know you are not intending the thing in the brackets to be a keyword but a variable, type etc.  if you wanted the code to work without them, try replacing [Enum] with System.Enum

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2014 10:56 AM