Visual Basic Enum Statement

In visual basic, Enum is a keyword and it is useful to declare an enumeration. In visual basic, the enumeration is a type and that will contain a set of named constants as a list.

 

By using enumeration, we can group constants that are logically related to each other. For example, the days of week can be grouped together by using enumeration in visual basic.

Visual Basic Enum Syntax

Following is the syntax of defining an enumeration using Enum keyword in a visual basic programming language.

 

Enum enum_name

' enumeration list

End Enum

If you observe the above syntax, we used Enum keyword to define the enumeration based on our requirements.

 

Following is the example of defining the enumeration using Enum keyword in visual basic programming language.

 

Enum Week

    Sunday

    Monday

    Tuesday

    Wednesday

    Thursday

    Friday

    Saturday

End Enum

If you observe above the example, we defined an enumeration “Week” with the list of named constants called enumeration list.

 

In visual basic, by default the first named constant in enumerator has a value of 0, and the value of each successive item in enumerator will be increased by 1. For example, in the above enumeration, Sunday value is 0, Monday is 1, Tuesday is 2, and so forth.

 

In case, if we want to change the default values of an enumerator, then assigning a new value to the first item in enumerator will automatically assign an incremental value to the successive items in an enumerator.

 

Following is the example of overriding the default values of an enumerator by assigning a new value to the first item in an enumerator.

 

Enum Week

    Sunday = 10

    Monday

    Tuesday

    Wednesday

    Thursday

    Friday

    Saturday

End Enum

In the above enumeration, the sequence of elements are forced to start from 10 instead of 0 like Sunday value is 10, Monday is 11, Tuesday is 12, and so forth.

 

To get the values of enum elements in visual basic, an explicit cast is necessary to convert from Enum type to an integral type.

 

For example, the following are the statements to get an enum item value by using a cast to convert from Enum to int.

 

Dim a As Integer = CInt(Week.Sunday)

Dim b As Integer = CInt(Week.Monday)

If you observe the above statements, we are doing the cast conversion (Enum to int) to get an enum item values.

Visual Basic Enum Example

Following is the example of declaring an enumeration using Enum keyword in a visual basic programming language.

 

Module Module1

    Enum Week

        Sunday

        Monday

        Tuesday

        Wednesday

        Thursday

        Friday

        Saturday

    End Enum

    Sub Main()

        Dim a As Integer = CInt(Week.Sunday)

        Dim b As Integer = CInt(Week.Monday)

        Dim c As Integer = CInt(Week.Tuesday)

        Console.WriteLine(Week.Sunday)

        Console.WriteLine(Week.Monday)

        Console.WriteLine("Sunday: {0}", a)

        Console.WriteLine("Monday: {0}", b)

        Console.WriteLine("Tuesday: {0}", c)

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined an enumeration Week and getting the values of enumeration items by explicitly converting from enum to int and assigned to an integer variables.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic Enum or Enumerator Example Result

 

If you observe the above result, the first enumerator (Sunday) has a value of 0 and the value of each successive enumerator is increased by 1.

Visual Basic Enum Methods

In visual basic, we have a class called Enum that contains the following helper methods to work with an enumeration (enum).

 

MethodDescription
Format It is useful to convert the value of enum type to a specified string format.
GetName It is useful to get the name of a specified enum item.
GetNames It is useful to get all item names of specified enum as an array.
GetValues It is useful to get all item values of the specified enum as an array.
Parse It is useful to convert the string representation of name or numeric value of one or more enumerated constants to an equivalent enumerated object.
GetUnderlyingType It is useful to return the underlying type of the specified enumeration.

Visual Basic Iterate through Enum

In visual basic, we can iterate or loop through an enum items using for or foreach loop to get the enumeration item names or values by using enum helper methods.

 

Following is the example of loop through an enum items to get all the item names and values using enum helper methods based on our requirements.

 

Module Module1

    Enum Week

        Sunday

        Monday

        Tuesday

        Wednesday

        Thursday

        Friday

        Saturday

    End Enum

    Sub Main()

        Console.WriteLine("Week Enumeration Values")

        For Each w As String In [Enum].GetNames(GetType(Week))

            Console.WriteLine(w)

        Next

        Console.WriteLine("Press Any Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are looping through an enumeration using the foreach loop and getting all the item names using GetNames() helper method.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic Iterate through Enum Example Result

 

If you observe the above result, we are able to get all the enumeration item names by looping through an enumeration using a foreach loop based on our requirements.