VB.NET Enum Examples

Enum type

You want to use the Enum type in your VB.NET program to represent a value that can have one of a selection of values. In an Enum type, you can specify different named constants, and these are then compiled into constants in your program, enabling you to reduce magic constants and improve program clarity.

These VB example programs use Enum values. Enums store named and magic constants.

Enum and Select

First, here we look at a Module in the VB.NET programming language 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. In the Main method, we use a Select statement on an enum variable; this enables a fast match for the correct Enum value.

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

If keyword

Frequently in your VB.NET programs, 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. Here, we glimpse at how you can represent a markup tag, such as HTML, in a conceptual model as an Enum value. Then, we test the tags with the If and ElseIf statements.

Program that uses enum if 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

In the VB.NET programming language, you will need to invoke the ToString method explicitly on the Enum variable when you pass it to the Console.Write or Console.WriteLine methods. 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.

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

Continuing with the conceptual representation of using Enum variables for HTML tags, we use a Stack collection to represent a markup tree here. If you have a HTML document with bold tags, hyperlink tags, and italics tags, you could use a Stack of Enum values (TagType) to represent to markup tree. You could use this functionality for HTML validation as well. To use the Stack collection, you typically use the Push, Pop, as well as Peek methods.

Program that uses enum and Stack collection [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

Summary

The VB.NET programming language

In this set of examples, we dived into the basics of the Enum type in the VB.NET language and saw how it can be used to represent a set of named constants. Typically, these named constants can represent a selection of options for a specific variable, and can then be used through the VB.NET program as symbolic constants. This improves the clarity of the code as well as its integrity and maintainability.

VB.NET Tutorials
.NET