VB.NET Enum

Array Class Collections File String Console Data Directive Enum Exception If Interface Lambda Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows

Enum type

An Enum type stores special values. These are named constants. With an Enum type we can remove magic constants throughout our program. And this improves code clarity. It makes the program easier to maintain.

Example

Important

First we look at a Module that uses an Enum named Importance. The Enum represents a constant value that tells how important the value is. The names of the constants in an Enum type can be changed to any valid identifiers.

Next:In the Main method, we use a Select statement on an enum variable. This enables a fast match for the correct Enum value.

Based on:

.NET 4.0

Program that uses Enum with Select: VB.NET

Module Module1

    ''' <summary>
    ''' Levels of importance.
    ''' </summary>
    Enum Importance
	Critical = 4
	Important = 3
	None = 0
	Regular = 2
	Trivial = 1
    End Enum

    Sub Main()
	Dim value As Importance = Importance.Critical
	' Select the enum and print a value.
	Select Case value
	    Case Importance.Trivial
		Console.WriteLine("Not true")
		Return
	    Case Importance.Critical
		Console.WriteLine("True")
		Exit Select
	End Select
    End Sub

End Module

Output

True

If, ElseIf

If keyword

Often you will use the If and ElseIf constructs to implement checks for certain Enum values. The If and ElseIf statements are only useful for when you are checking an Enum Dim variable against certain named constants in that Enum type.

If Then

Here:We represent a markup tag, such as HTML, in a conceptual model as an Enum value. We test the tags with the If and ElseIf statements.

Program that uses enum If and ElseIf: VB.NET

Module Module1

    ''' <summary>
    ''' Represents tag type.
    ''' </summary>
    Enum TagType
	BoldTag = 1
	HyperlinkTag = 3
	ItalicsTag = 2
	None = 0
    End Enum

    Sub Main()
	' Create enum type variable.
	Dim value As TagType = TagType.HyperlinkTag
	' Use enum in If-statement.
	' ... Also use ElseIf statement.
	If value = TagType.BoldTag Then
	    Console.WriteLine("Bold")
	ElseIf value = TagType.HyperlinkTag Then
	    Console.WriteLine("Not true")
	End If
    End Sub

End Module

Output

Not true

ToString

String type

We need to invoke ToString explicitly on the Enum variable when you pass it to the Console.Write or WriteLine subs. If you do not call ToString, you will receive the numeric value of the enum, not the string representation.

In this example, we demonstrate the ToString method on the Enum variable type.
The Enum represents a type of animal,
which contains options for cats
and dogs. We finally call Console.WriteLine.

Console
Program that uses enum and ToString: VB.NET

Module Module1

    ''' <summary>
    ''' Type of animal.
    ''' </summary>
    Enum AnimalType
	Cat = 1
	Dog = 2
	None = 0
    End Enum

    ''' <summary>
    ''' Type of visibility.
    ''' </summary>
    Enum VisibilityType
	Hidden = 2
	None = 0
	Visible = 4
    End Enum

    Sub Main()
	Dim dog As AnimalType = AnimalType.Dog
	Dim hidden As VisibilityType = VisibilityType.Hidden
	' Write to the Console with the ToString method.
	Console.WriteLine(dog.ToString)
	Console.WriteLine(hidden.ToString)
    End Sub

End Module

Output

Dog
Hidden

Enum Stack

Stack

Here we use a Stack collection to represent a markup tree. If you have an HTML document with bold tags, hyperlink tags, and italics tags, you could use a Stack of Enum values (TagType) to represent to markup tree.

Note:You could use this functionality for HTML validation as well. Opening and closing tags could be matched.

Tip:To use the Stack collection, you typically use the Push, Pop, as well as Peek methods.

Stack
Program that uses enum, Stack: VB.NET

Module Module1

    Enum TagType
	BoldTag = 1
	HyperlinkTag = 3
	ItalicsTag = 2
	None = 0
    End Enum

    Sub Main()
	' Create a new Stack generic instance.
	Dim stack As New Stack(Of TagType)
	' Add a BoldTag to it.
	stack.Push(TagType.BoldTag)
	' Add an ItalicsTag to it.
	stack.Push(TagType.ItalicsTag)
	' Pop the top element.
	' ... Then write it to the Console.
	Dim popped As TagType = stack.Pop()
	Console.WriteLine(popped)
	Console.WriteLine(popped.ToString)
    End Sub

End Module

Output

2
ItalicsTag

Parse

Note

If you have a String value that contains an Enum name, how can you convert it to an Enum value? The Enum.Parse and Enum.TryParse functions are available for this purpose. We test them in example programs.

Enum.Parse, TryParse

Summary

We dived into the basics of the Enum type in the VB.NET language. We saw how it can be used to represent a set of named constants. Typically these named constants represent a selection of options for a specific variable.

And:They are used throughout the VB.NET program as symbolic constants. This improves the clarity of the code.

VB.NET