Daily Archives: November 11, 2020
VB.NET || How To Set & Get The Description Of An Enum Using VB.NET
The following is a module with functions which demonstrates how to set and get the description of an enum using VB.NET.
This function uses the DescriptionAttribute to get the description of an Enum element. If a description attribute is not specified, the string value of the Enum element is returned.
1. Get Description
The example below demonstrates the use of ‘Utils.GetDescription‘ to get the description of enum elements.
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 |
' Get Description Imports Utils Public Enum MimeTypes <System.ComponentModel.Description("application/pdf")> ApplicationPDF = 1 <System.ComponentModel.Description("application/octet-stream")> ApplicationOctect = 2 <System.ComponentModel.Description("application/zip")> ApplicationZip = 3 <System.ComponentModel.Description("application/msword")> ApplicationMSWordDOC = 4 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")> ApplicationMSWordDOCX = 5 <System.ComponentModel.Description("application/vnd.ms-excel")> ApplicationMSExcelXLS = 6 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")> ApplicationMSExcelXLSX = 7 End Enum For Each value As MimeTypes In System.[Enum].GetValues(GetType(MimeTypes)) Debug.Print($"Numeric Value: {CInt(value)}, Name: {value.ToString}, Description: {value.GetDescription}") Next ' expected output: ' Numeric Value: 1, Name: ApplicationPDF, Description: application/pdf ' Numeric Value: 2, Name: ApplicationOctect, Description: application/octet-stream ' Numeric Value: 3, Name: ApplicationZip, Description: application/zip ' Numeric Value: 4, Name: ApplicationMSWordDOC, Description: application/msword ' Numeric Value: 5, Name: ApplicationMSWordDOCX, Description: application/vnd.openxmlformats-officedocument.wordprocessingml.document" ' Numeric Value: 6, Name: ApplicationMSExcelXLS, Description: application/vnd.ms-excel ' Numeric Value: 7, Name: ApplicationMSExcelXLSX, Description: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
2. 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 30 31 32 33 34 |
' ============================================================================ ' 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 the description of an enum decorated with a DescriptionAttribute ''' </summary> ''' <param name="value">An Enumeration value</param> ''' <returns>The description of an enumeration value</returns> <Runtime.CompilerServices.Extension()> Public Function GetDescription(value As System.[Enum]) As String Dim description = value.ToString Dim attribute = GetAttribute(Of System.ComponentModel.DescriptionAttribute)(value) If attribute IsNot Nothing Then description = attribute.Description End If Return description End Function Public Function GetAttribute(Of T As {System.Attribute})(value As System.[Enum]) As T Dim field = value.GetType().GetField(value.ToString()) Dim attribute = CType(field.GetCustomAttributes(GetType(T), False), T()).FirstOrDefault Return attribute End Function End Module End Namespace ' http://programmingnotes.org/ |
3. 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
' ============================================================================ ' 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 Imports Utils Module Program Public Enum MimeTypes <System.ComponentModel.Description("application/pdf")> ApplicationPDF = 1 <System.ComponentModel.Description("application/octet-stream")> ApplicationOctect = 2 <System.ComponentModel.Description("application/zip")> ApplicationZip = 3 <System.ComponentModel.Description("application/msword")> ApplicationMSWordDOC = 4 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")> ApplicationMSWordDOCX = 5 <System.ComponentModel.Description("application/vnd.ms-excel")> ApplicationMSExcelXLS = 6 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")> ApplicationMSExcelXLSX = 7 <System.ComponentModel.Description("application/vnd.ms-powerpoint")> ApplicationMSPowerPointPPT = 8 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.presentationml.presentation")> ApplicationMSPowerPointPPTX = 9 <System.ComponentModel.Description("audio/basic")> AudioBasic = 10 <System.ComponentModel.Description("audio/mpeg")> AudioMPEG = 11 <System.ComponentModel.Description("audio/x-wav")> AudioWAV = 12 <System.ComponentModel.Description("image/gif")> ImageGIF = 13 <System.ComponentModel.Description("image/jpeg")> ImageJPEG = 14 <System.ComponentModel.Description("image/png")> ImagePNG = 15 <System.ComponentModel.Description("text/html")> TextHTML = 16 <System.ComponentModel.Description("text/plain")> TextPlain = 17 <System.ComponentModel.Description("video/mpeg")> VideoMPEG = 18 <System.ComponentModel.Description("video/x-msvideo")> VideoAVI = 19 End Enum Sub Main(args As String()) Try For Each value As MimeTypes In System.[Enum].GetValues(GetType(MimeTypes)) Display($"Numeric Value: {CInt(value)}, Name: {value.ToString}, Description: {value.GetDescription}") 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.
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.