What are enumerations in Visual Basic/C#?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    What are enumerations in Visual Basic/C#?

    Enumerations are constant values that you can define in your application. There are many benefits of using enumerations in certain situations, including greater readability and reduced code clutter because all related values can be defined in an enumeration. An example enumerated list:

    Code:
    Public Class Demo
        Enum Groups
            Member
            Moderator
            Administrator
        End Enum 
    
        Public Sub Output()
           TxtBlock.Text = Groups.Member
        End Sub
    End Class
    You can also declare your own variables with a data type of the Enum created above. For example:

    Code:
    Public Class Demo
        Enum Groups
            Member
            Moderator
            Administrator
        End Enum 
    
        Public Sub Output()
           Dim GroupList As Groups = Groups.Moderator
           TxtBlock.Text = GroupList
        End Sub
    End Class
    The examples above use Visual Basic, but the concept remains much the same in C#:

    Code:
    public enum Groups
    {
       Member,
       Moderator,
       Administrator
    }
Working...
X