How to use Enum in vb.net

When you are in a situation to have a number of constants that are logically related to each other, you can define them together these constants in an enumerator list. An enumerated type is declared using the enum keyword.

An enumeration comprises a designated name, an underlying data type, and a collection of members, with each member representing a constant value. This construct proves particularly valuable when working with a predefined set of values that hold functional significance and remain fixed throughout the program's execution. By defining an enumeration, developers can establish a clear and meaningful representation of these constant values, enhancing code readability and maintainability. The underlying data type of the enumeration ensures type safety and facilitates seamless integration with other components of the program.

Syntax: enum vb.net Enum declaration
Enum Temperature Low Medium High End Enum

A simple Enum Excercise

Public Class Form1 Enum Temperature Low Medium High End Enum Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim value As Temperature = Temperature.Medium If value = Temperature.Medium Then MsgBox("Temperature is Mediuam..") End If End Sub End Class

Initializing Members

By default, each element in the enum is assigned an underlying type of int. If you do not explicitly specify an initializer for a member, VB.Net automatically initializes it to zero. To illustrate this behavior, let's consider the previous example and attempt to convert the enum values to integers. The result would be as follows:

Dim value As Temperature = Temperature.Medium Dim val As Integer = CInt(value) Console.WriteLine("Temperature value is.." + val)

Output is : Temperature value is..1

Retrieve and check the Enum value

Dim value As Temperature = Temperature.Medium If value = Temperature.Medium Then Console.WriteLine("Temperature is Mediuam..") End If

When declaring an enum, if you assign a specific value to the first member, the subsequent members will be automatically assigned values greater by one than that of the immediately preceding member. You can observe this behavior in the following program:

Public Class Form1 Enum Temperature Low = 3 Medium High End Enum Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim value As Temperature = Temperature.Medium Dim val As Integer = CInt(value) MsgBox("Temperature value is.." + val.ToString) End Sub End Class

Output is : Temperature value is..4

You can specify another integral numeric type by using a colon. The following Enum declare as byte, you can verify the underlying numeric values by casting to the underlying type.

Enum Temperature As Byte Low Medium High End Enum

You can retrieve the value like the following:

Dim value As Temperature = Temperature.Medium Dim val As Byte = CByte(value) Console.WriteLine("Temperature value is.." + val);

How to get int value from enum

Private Enum Days Sunday = 1 TuesDay = 2 wednesday = 3 End Enum 'get int val Private day As Integer = CInt(Days.TuesDay)

Enum.Parse in VB.Net

The Enum.Parse() method in VB.Net allows you to convert a string representation or integer value of one or more enumerated constants into an equivalent Enum object. This method enables you to dynamically convert a string or integer value into its corresponding Enum value during runtime. By specifying the appropriate Enum type and the string or integer value, Enum.Parse() performs the conversion and returns an Enum object that represents the specified value. This functionality is particularly useful when working with user input or when you need to convert values between different representations within your code.

String convert to Enum

Public Class Form1 Public Enum Colors red blue green yellow End Enum Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim inVal As String = "green" Dim newColor As Colors = DirectCast([Enum].Parse(GetType(Colors), inVal), Colors) 'Check the Enum type If newColor = Colors.green Then MessageBox.Show(newColor.ToString()) End If End Sub End Class

Output is : green

How can an int value cast to enum

Following is the easiest method to cast an int to enum.

MyEnum myenum = (MyEnum)intvalue;

Ex:

Private Enum Days Sunday = 1 TuesDay = 2 wednesday = 3 End Enum //cast here Private day As Days = DirectCast(3, Days)

Converts int to enum values

Method 2:

MyEnum myenum = (MyEnum)Enum.ToObject(typeof(MyEnum) , intvalue);

Dim day As Days = DirectCast([Enum].ToObject(GetType(Days), 3), Days)

You can check if it's in range using Enum.IsDefined

If [Enum].IsDefined(GetType(Days), day) Then MessageBox.Show("Its in tange") End If

How to Loop through all enum values in VB.Net

The GetValues() method in VB.Net allows you to retrieve an array that contains a value for each member of the enum Type. This method provides a convenient way to access and iterate over all the possible values of an enum. The array returned by GetValues() includes a value for each distinct member of the enum, even if multiple members have the same value. This means that if there are duplicate values within the enum, the resulting array will include duplicate values as well. This behavior ensures that all possible enum values are accounted for and accessible through the returned array.

Iterating through an enum in vb.net

The following example will show how do enumerate an enum .

Public Class Form1 Public Enum Colors red blue green yellow End Enum Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each iColor As Colors In [Enum].GetValues(GetType(Colors)) MessageBox.Show(iColor.ToString()) Next End Sub End Class

Enum in Select Case

The following program shows how to use Enum in Select....Case statement.

Full Source VB.NET
Public Class Form1 Enum Temperature Low Medium High End Enum Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim value As Temperature = Temperature.Medium Select Case value Case Temperature.Low MessageBox.Show("Low Temperature") Exit Select Case Temperature.Medium MessageBox.Show("Mediuam Temperature") Exit Select Case Temperature.High MessageBox.Show("High Temperature") Exit Select End Select End Sub End Class

Conclusion

An Enum can be used to define a set of related constants, providing a structured and readable way to work with fixed values, such as days of the week or status codes, by assigning a name and underlying value to each constant.