By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Dec 31 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"herbert" wrote:
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
Dec 31 '06 #2

P: n/a
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
Jan 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.