VB.NET Enum Statement

VB.NET has a keyword Enum, which is also named the Enumeration. The Enumeration is a user-defined data type used in defining a related set of constants as a list with the help of the keyword enum statement. We can also use Enum with structure, module, class, and procedure.

Enum type stores some special values. Such special values are termed as constants.With the help of Enum keyword, one could change the magic constants within the whole program. With the use of Enum Keyword, an enumerated type can be declared.

Syntax – Enum Statement:

In this Syntax –

Attribute ListList of Attributes applied to variable
Access ModifierSpecify code to access the enumerations.
ShadowsEnum hiding program items of same name in base class.
Enumeration nameName of Enumeration
DataTypeDescribing DataType of Enumeration.
Member ListList of member constants defined in statement.

Syntax: Member List of Enumeration

In this,

name– defines name of member.

initializer– value provided to the enumeration member.

Declaring the Enumerated Data.

The data declared below is enumerated with the help of Enum Keyword.

1. Enum Vegetables

2. Potato

3. Onion

4. Tomato

5. Peas

6. Ginger

7. End Enum

Vegetables isthe enumerated data containing list of Vegetables called Enumeration list.

The Enumeration List assigns a value 0 to theinitial index of enumerator.Then,each upcoming element in enumerator list increases by 1.

Example – In the above list, the value of Potato is 0, the value of Onion is 1, the value of Tomato is 2.

Ifwe create new value for the list’s first index, and later the enumerator’s values can provide a new increasing value for successor objects in the enumerator list automatically.

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

1. Enum Vegetables

2. Potato = 4

3. Onion

4. Tomato

5. Peas

6. Ginger

7. End Enum

In this, the value of index for the initial element in the enumeration list is 5. Thus, the initial values in the list are – Potato = 4, Onion = 5, Tomato = 6 and so on. Hence, the new value can be provided in the mid of enum list. Then, it follows a previously defined sequence set.

Enum Methods

These methods are available in Enum class, containing various methods to work with an enumeration (Enum).

Format –This method converts the enum type value to string.

GetName–This method gets the element name from the Enum list.

GetNames – This method helps in obtaining all available names from enumeration list in the form of an array.

GetValues  –This method helps in achieving all values from enumeration list in the form of an array.

Parse –This method helps in converting representation of string of a name or a numeric value of anenum constant into anenum object.

Get Underlying Type –This method helps in returning underlying item’s type in the enumeration.

Loop in Enum

The For and For Each loop in Enum can be used to fetch the values and names of every element of enumerator list with helper methods of enum.

Example – Write a program to get all the names and values of enumeration elements using the loop in enum helper methods.



VB.NET Enum Statement

The execution of the data values in program is performed continuously in the For Each Loop till the list becomes empty. Value of the variable i is increased at every iteration. The GetNames function will get each item from the enumerated list and print the enum item.

Pin It on Pinterest

Share This