Enumeration: Returning the "next" item, round robin-style

G

Guest

how do I code generic functions to return the next item in an enumeration
a) sorted by name, b) sorted by value c) sorted by declaration
in a round-robin style ?

for example the enum is
Enum Colors
Red = 5
Green = 7
Blue = 16
Yellow = 2
End Enum 'Colors

and the generic functions allow me to code:

Dim next as Colors = GetNextEnumItemByName(colors, colors.green) 'returns
Colors.Red

Dim next as Colors = GetNextEnumItemByValue(colors, colors.green) 'returns
Colors.Blue

Dim next as Colors = GetNextEnumItem(colors, colors.yellow) 'returns
Colors.red

I tried
Private Function GetNextEnumValue(Of T)(ByVal start As T) As T
'get the next (round-robin style) value of an enumeration
Dim enumvalues() As String = [Enum].GetValues(GetType(Colors)) 'get
all enum values
Dim index As Integer = Array.IndexOf(enumvalues, start) 'find the
index of the start value
Dim target As Integer = (index + 1) Mod enumvalues.Length 'this is
the index of the "next" value
Return CType([Enum].Parse(T.GetType, "Red"), T) '*** THIS T.GetType
FAILS ***
End Function

thank you very much. herbert
 
G

Guest

How about:
Private Shared Function GetNextEnumValue(Of T As { Structure, IComparable
})(ByVal currentValue As T, ByRef nextValue As T) As Boolean
If Not TypeOf currentValue Is Enum Then
Throw New ArgumentException("Not Enum", "currentValue")
End If
Dim values As T() = TryCast(Enum.GetValues(GetType(T)),T())
If (Not values Is Nothing) Then
Dim i As Integer = 0
Do While (i < (values.Length - 1))
If (values(i).CompareTo(currentValue) = 0) Then
nextValue = values((i + 1))
Return True
End If
i += 1
Loop
End If
Return False
End Function
 
G

Guest

Thank you, Peter.
This is my solution for all the VB.NET fans out there:


Private Function GetNextEnum(Of T)(ByVal start As T) As T
'get the next (round-robin style) value of an enumeration; the
sequence is as defined in the enumeration
Dim enumValues() As T = [Enum].GetValues(GetType(Colors)) 'get all
enum values
Dim index As Integer = Array.IndexOf(enumValues, start) 'find the
index of the start value
Dim target As Integer = (index + 1) Mod enumValues.Length 'this is
the index of the "next" value
Return enumValues(target)
End Function

Private Function GetNextEnumByValue(Of T)(ByVal start As T) As T
'get the next (round-robin style) value of an enumeration; the
sequence is defined by the values of the num items
Dim enumValues() As T = [Enum].GetValues(GetType(Colors)) 'get all
enum values
Array.Sort(enumValues) 'sort
the array
Dim index As Integer = Array.IndexOf(enumValues, start) 'find the
index of the start value
Dim target As Integer = (index + 1) Mod enumValues.Length 'this is
the index of the "next" value
Return enumValues(target)
End Function

Private Function GetNextEnumByName(Of T)(ByVal start As T) As T
'get the next (round-robin style) value of an enumeration; the
sequence is as defined by the names of the enum items
Dim enumNames() As String = [Enum].GetNames(GetType(Colors)) 'get
all names of the items in the enum
Array.Sort(enumNames) 'sort the array of enum names by name
'find the index of the start value
Dim index As Integer = Array.IndexOf(enumNames, start.ToString)
'get the next item in the array
Dim targetIndex As Integer = (index + 1) Mod enumNames.Length 'this
is the index of the "next" value
Return [Enum].Parse(GetType(T), enumNames(targetIndex))
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top