Javatpoint Logo
Javatpoint Logo


In VB.NET, Enum is a keyword known as Enumeration. Enumeration is a user-defined data type used to define a related set of constants as a list using the keyword enum statement. It can be used with module, structure, class, and procedure. For example, month names can be grouped using Enumeration.


In the above syntax, the enumeration name is a valid identifier name, and the data type can be mentioned if necessary, the enumeration data_list or Enum member list represents the set of the related constant member of the Enumeration.

Declaration of Enumerated Data

Following is the declaration of enumerated data using the Enum keyword in VB.NET Programming language:

Here, Fruits is the name of enumerated data that contains a list of fruits called enumeration lists.

When we are creating an Enumerator constant data list, by default, it assigns a value 0 to the first index of the enumerator. Each successive item in the enumerator list is increased by 1. For example, in the above enumerator list, Banana value is 0, Mango value is 1, Orange value is 2, and so on.

Moreover, if we want to assign a new value to the default value of the enumerator items, set a new value to the list's first index, and then enumerator's values can automatically provide a new incremental value for successor objects in the enumerator list.

Override a default index value by setting a new value for the first element of the enumerator list.

Here, we have assigned 5 as the new index value of the first item in an enumerator list. So, the initial value of the list's Banana=5, Mango=6, orange=7, and so on. Furthermore, we can also provide new value in the middle of the enumerator list, and then it follows the sequence set by the previous item.

Example 1: Write a program to understand the uses of enumeration data in VB.NET.




In the above example, an enumerator "Number" that gets each value of the enumerated data such as "Number.One", etc. to print the list.

Get an integer value from Enum

To get the integer value of the enumerator items from a list in VB.NET. We can use an explicit CInt() method to get the Enum data index value is an integer. For example, the following statement shows how to change an Enum item value using CInt () to convert from Enum to Integer.

Here, Fruits is the name of enumerator data and the Orange, and Apple is the name of element define in the enumerator list to access the index number of the items.

Example 2: Write a program to set and access the index number using the Enum in VB.NET.




In the above example, we have assigned a new value to the first element of the enumeration list (January = 1), and then the successive values of the list are incremented by 1. And in the middle, we have again set a new value to the item (September = 10) to get each item's index value defined in the enumeration list.

VB.NET Enum Methods

The following methods are available in Enum class that contain various methods to work with an enumeration (enum).

Methods Description
Format A Format method is used to convert an enum type value to a specified string format.
GetName As the name suggests, the GetName function is used to get the specified item's name from the enumeration list.
GetNames A GetNames method is used to retrieve all available names from a specified enumeration list as an array.
GetValues As the name suggests, a GetValues method is used to obtain all values from a specified enumeration list as an array.
Parse A parse method is used to convert a string representation of a name or numeric value of one or more enumerated constants into an enumerated object.
GetUnderlyingType It is used to return the type of the underlying item in a specified enumeration.

Use of loop in Enum

In VB.NET, we can use For or Foreach loop in enum to get the name or values of each item of an enumerator list with enum helper methods.

Example 3: Write a simple program to get all the enumeration item names and values using the loop in enum helper methods.




In the above program, the For Each loop is continuously executes their enumerated data values until the list does not become empty. At each iteration, the value of the variable i is incremented, and the GetNames function will get each item from the enumerated list and prints the enum item.

Next TopicVB.NET String

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA