Array Class Collections File String Cast Console Data Directive Enum Exception If Interface Lambda LINQ Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows
An Enum type stores special values. These are named constants. With an Enum, we replace magic constants throughout a program.
This improves code clarity,
and makes code easier to maintain.
We look first at a Module that uses an Enum named Importance. The Enum represents a constant value. It indicates a value's priority. The names of the constants in an Enum can be any valid identifiers.
Select:In the Main method, we use a Select statement on an enum variable. This enables a fast match for the correct Enum value.
Result:The case for Importance.Critical is selected. The result of the program is true.
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
Often we use the If and ElseIf constructs to implement checks for certain Enum values. With conditionals, we check an Enum Dim variable against named constants in an Enum type. Other values are not checked.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
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.
Here:In this example, we demonstrate the ToString method on the Enum variable type.
Animal:The Enum represents a type of animal, which contains options for cats and dogs. We finally call Console.WriteLine.Console
Program that uses ToString, Enum: 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
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, we typically use the Push, Pop as well as Peek methods.Stack
Program that uses Stack, Enum: 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
An Enum has a default value. This corresponds to the value that equals 0 within the Enum type, which is by default the first listed one. When part of a class, an Enum does not need initialization to this default value.
The default values of an enum proceed up from 0.
So the first is zero,
the second is one,
and the third is two. Initializing an Enum to zero is a performance negative. It will result in additional yet unneeded steps.
Program that uses Enum, default value: VB.NET Module Module1 Enum Level Normal Low High End Enum Class Item ''' <summary> ''' Public enum. ''' </summary> Public _level As Level End Class Sub Main() ' The default value for an enum is the one equal to 0. Dim v As Item = New Item Console.WriteLine(v._level.ToString()) End Sub End Module Output Normal
Enums have a backing type—a data representation like Integer or Byte. By default, Enums are represented as Integers. But we can, with the "As" keyword change this. Here we create a Byte Enum.Byte
Tip:This ability is useful when designing a Class that is instantiated many times in memory.
Tip 2:With smaller types, we can reduce memory usage. Four Bytes are packed into the size occupied by one Integer.Integer
Program that uses enum backing type, byte: VB.NET Module Module1 Enum Code As Byte Small = 0 Medium = 1 Large = 2 End Enum Sub Main() ' The value is represented in a byte. Dim value As Code = Code.Medium Console.WriteLine(value) End Sub End Module Output 1
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
Enums are an important feature. They are used to represent a set of named constants. Typically, these named, symbolic constants are a selection of options for a specific variable.