VB.NET || How To Iterate & Get The Values Of An Enum Using VB.NET
The following is a module with functions which demonstrates how to iterate and get the values of an enum using VB.NET.
This function is a generic wrapper for the Enum.GetValues function.
1. Get Values – Basic Enum
The example below demonstrates the use of ‘Utils.GetEnumValues‘ to get the values of a simple Enum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
' Get Values - Basic Enum Enum Colors Red Green Blue Yellow End Enum Dim colors = Utils.GetEnumValues(Of Colors) For Each color In colors Debug.Print($"Numeric Value: {CInt(color)}, Name: {color.ToString}") Next ' expected output: ' Numeric Value: 0, Name: Red ' Numeric Value: 1, Name: Green ' Numeric Value: 2, Name: Blue ' Numeric Value: 3, Name: Yellow |
2. Get Values – Numbered Enum
The example below demonstrates the use of ‘Utils.GetEnumValues‘ to get the values of a numbered Enum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
' Get Values - Numbered Enum Public Enum Pets None = 0 Dog = 1 Cat = 2 Rodent = 4 Bird = 8 Fish = 16 Reptile = 32 Other = 64 End Enum Dim pets = Utils.GetEnumValues(Of Pets) For Each pet In pets Debug.Print($"Numeric Value: {CInt(pet)}, Name: {pet.ToString}") Next ' expected output: ' Numeric Value: 0, Name: None ' Numeric Value: 1, Name: Dog ' Numeric Value: 2, Name: Cat ' Numeric Value: 4, Name: Rodent ' Numeric Value: 8, Name: Bird ' Numeric Value: 16, Name: Fish ' Numeric Value: 32, Name: Reptile ' Numeric Value: 64, Name: Other |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 11, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Returns a List of the values of the constants in a specified enumeration ''' </summary> ''' <returns>The values contained in the enumeration</returns> Public Function GetEnumValues(Of T)() As List(Of T) Dim type = GetType(T) If Not type.IsSubclassOf(GetType(System.[Enum])) Then Throw New InvalidCastException($"Unable to cast '{type.FullName}' to System.Enum") End If Dim result = New List(Of T) For Each value As T In System.[Enum].GetValues(type) result.Add(value) Next Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 11, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Module Program Enum Colors Red Green Blue Yellow End Enum Public Enum Pets None = 0 Dog = 1 Cat = 2 Rodent = 4 Bird = 8 Fish = 16 Reptile = 32 Other = 64 End Enum Sub Main(args As String()) Try Dim colors = Utils.GetEnumValues(Of Colors) For Each color In colors Display($"Numeric Value: {CInt(color)}, Name: {color.ToString}") Next Display("") Dim pets = Utils.GetEnumValues(Of Pets) For Each pet In pets Display($"Numeric Value: {CInt(pet)}, Name: {pet.ToString}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply