VB.NET Enumeration


  • The most basic user defined data type is Enumeration.
  • It arranges a list of items in a sequence and associates a symbolic number to each element.
  • We can say Enumeration is a user defined data type which provides a way for associating a number with a name.
  • In VB.NET we use Enum keyword to declare Enumeration.

Declaration of Enumeration


  • Declaration of Enumeration is done by using Enum keyword.
  • The basic syntax to declare an Enumeration data type is as follows:
  • Enum Enum_name
    List of elements
    End Enum

  • Enum is an keyword of integer data type. Each value in Enum corresponds to integer started from 0. The simple example of Enumeration is as follows:
  • Enum trafficlight
    Red
    Green
    Yellow
    End Enum

  • According to the above example value stored for these Enum elements as:
  • Red=0
    Green=1
    Yellow=2

  • You can also assign your own values to elements of Enum as given below:
  • Enum trafficlight Red=5
    Green=6
    Yellow=7
    End Enum

  • Here is an example which is sufficient to understand the Enumeration:
  • Example

    Module Module1
    Enum trafficlight
    Red
    Green
    Yellow
    End Enum
    Sub Main()
    Console.WriteLine("Red=" & trafficlight.Red)
    Console.WriteLine("Green=" & trafficlight.Green)
    Console.WriteLine("Yellow=" & trafficlight.Yellow)
    Console.ReadLine()
    End Sub
    End Module

  • The Output of the above program will be as follows:
  • Output

    Red=0
    Green=1
    Yellow=2