Net-informations.com
SiteMap  | About    

How to use Enum in vb.net

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.

Syntax:

enum vb.net

Enum declaration

Enum Temperature Low Medium High End Enum

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

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

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 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:

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

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.

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

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

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 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 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 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
		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






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.