How to use Enum in

VB.NET Enum Example

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.



Enum declaration

An enumeration has a name, an underlying data type, and a set of members. Each member represents a constant. It is useful when you have a set of values that are functionally significant and fixed.

Retrieve and check the Enum value

A simple Enum Excercise

Initializing Members

By default the underlying type of each element in the enum is int. If you do not specify initializer for a member, VB.Net initializes it either to zero. If you try with above example to convert to integer then you can see the result like the following:

Output is : Temperature value is..1

If you declare a different value in the first member of Enum then it assign the next value greater by one than that of the immediately preceding member. Check with the following program.

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.

You can retrieve the value like the following:

How to get int value from enum

Enum.Parse in VB.Net

Enum.Parse() converts the VB.Net string representation of the name or integer value of one or more enumerated constants to an equivalent Enum object.

String convert to Enum

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;


Converts int to enum values

Method 2:

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

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

How to Loop through all enum values in VB.Net

The VB.Net GetValues() returns an array that contains a value for each value of the enum Type . If more than one members have the same value, it will return array includes duplicate values

Iterating through an enum in

The following example will show how do enumerate an enum .

Enum Flags Attribute

The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. More about.... Enum Flags Explained

Convert an enum to a List

The following program shows how to convert an enum to List Datastructure. More about.... Enum to List

Enum in Select Case

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

Public Class Form1
	Enum Temperature
	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 (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About