0 Replies - 1018 Views - Last Post: 02 February 2016 - 09:23 AM

#1 TechnoBear  Icon User is offline

  • Lady A
  • member icon

Reputation: 185
  • View blog
  • Posts: 818
  • Joined: 02-November 11

Obtain All Enum Values

Posted 02 February 2016 - 09:23 AM

This method allows a quick way to enter into a loop, or to otherwise obtain an Array, of Enum values. This comes from an instance where I had to populate a series of DropDownLists From Enum Objects, creating this snippet saved rewriting common code. I am not aware of any framework restrictions for System.Enum.

Snippet:
''' <summary>
''' Get the Values of any Enum
''' </summary>
''' <typeparam name="T">Enum to Get Values From</typeparam>
''' <returns>System.Array With Enum Values</returns>
''' <remarks></remarks>
Public Function GetValues(Of T)() As System.Array
   'Check If Supplied Object is an Enum Object
   If GetType(T).BaseType <> GetType(System.Enum) Then
       'Failed Check Results in an Argument Exception to Stop Unhandled Exceptions
       Throw New ArgumentException("T must be of type System.Enum")
   End If

   'Return Values of given Enum as System.Array
   Return System.Enum.GetValues(GetType(T))
End Function


Basic Example Usage:
Public Enum AquisitionType
    NORMAL
    PEAK
    AVERAGE
    HRESOLUTION
End Enum


For Each AquisitionType As AgilentMSOX2012A.AquisitionType In GetValues(Of AgilentMSOX2012A.AquisitionType)()
   txtAquisitionType.Text = AquisitionType.ToString() & VbNewline
Next


Output:
NORMAL
PEAK
AVERAGE
HRESOLUTION


Is This A Good Question/Topic? 0
  • +

Page 1 of 1