473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 = GetNextEnumItem ByName(colors, colors.green) 'returns
Colors.Red

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

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

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

thank you very much. herbert
Dec 31 '06 #1
2 3831
How about:
Private Shared Function GetNextEnumValu e(Of T As { Structure, IComparable
})(ByVal currentValue As T, ByRef nextValue As T) As Boolean
If Not TypeOf currentValue Is Enum Then
Throw New ArgumentExcepti on("Not Enum", "currentVal ue")
End If
Dim values As T() = TryCast(Enum.Ge tValues(GetType (T)),T())
If (Not values Is Nothing) Then
Dim i As Integer = 0
Do While (i < (values.Length - 1))
If (values(i).Comp areTo(currentVa lue) = 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 = GetNextEnumItem ByName(colors, colors.green) 'returns
Colors.Red

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

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

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

thank you very much. herbert
Dec 31 '06 #2
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(GetT ype(Colors)) 'get all
enum values
Dim index As Integer = Array.IndexOf(e numValues, start) 'find the
index of the start value
Dim target As Integer = (index + 1) Mod enumValues.Leng th 'this is
the index of the "next" value
Return enumValues(targ et)
End Function

Private Function GetNextEnumByVa lue(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(GetT ype(Colors)) 'get all
enum values
Array.Sort(enum Values) 'sort
the array
Dim index As Integer = Array.IndexOf(e numValues, start) 'find the
index of the start value
Dim target As Integer = (index + 1) Mod enumValues.Leng th 'this is
the index of the "next" value
Return enumValues(targ et)
End Function

Private Function GetNextEnumByNa me(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(GetTy pe(Colors)) 'get
all names of the items in the enum
Array.Sort(enum Names) 'sort the array of enum names by name
'find the index of the start value
Dim index As Integer = Array.IndexOf(e numNames, start.ToString)
'get the next item in the array
Dim targetIndex As Integer = (index + 1) Mod enumNames.Lengt h 'this
is the index of the "next" value
Return [Enum].Parse(GetType( T), enumNames(targe tIndex))
End Function
Jan 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3722
by: joe | last post by:
Can anyone help me out by either telling me how to get to the result i need or by pointing me to some documentation about the following question: A certain query to a database give me eg 100 results: $query = "select id, name, addr1, addr2, city from mytable where id=" ..$id; $connection = mysql_connect(etc etc) ....
8
17051
by: Adam Nemitoff | last post by:
Is is possible to construct a SELECT statement that contains a WHERE clause that uses the value from a column in the "next" row? ie. given a table with a single field named "myField" with the following values I want a SELECT statement that selects the rows "WHERE myField='1' and myField = '2' ": 5 6 1
1
2008
by: tnhoe | last post by:
Hi, <Form method='post' action="next.htm?btn="+"this.myform.myobj.value"> What is the correct syntax for above ? Regards Hoe
2
1870
by: John Smith | last post by:
I have more than 4,000 pieces of email in my Yahoo account. I would like to write a program to download them all. In order to write such a program, I need to know a few things. 1. After I fill out userid, password, and click on "login", what exactly is sent to Yahoo? 2. When I click on "next" to fetch the next mail, what exactly is sent to Yahoo?
1
3660
by: midiweb01 | last post by:
Sorry for my bad english here! I try my best. I would like to make a slideshow for my site about paintings. I have four menus: Portraits-landscapes-Still Life-Animals Each Menu has his own slideshows with a certain number of images. What Iwant is clear and simple. I don't want to display all the images. I need a javascript code that displays each image enlarged on the right and so on for each one through a slidebar "Next" and "PreviOus"...
3
2224
by: domcatanzarite | last post by:
How would one create a button that on click advances the form to the next "non recurring record" as opposed to the next record. The field the button needs to que from has groups of duplicate values. I need it to advance the form to the next group of duplicate values not just the next record.
3
1815
by: Dale | last post by:
Access 2000 I am trying to check the form to be sure that required fields are entered. For each required field (Control) I have set the tag property to "1". I am trying to loop through all controls with a tag property of "1", If they are null, I want to stop the loop and setfocus on the control. The below code works Except it will not stop looping when a control is null. It will display the msgBox and then continue to the next...
11
48006
by: Maxwell2006 | last post by:
Hi, I know that this is not a good practice, but I wonder do we have "on error resume next" in C#? Thanks,
10
4798
by: craig.keightley | last post by:
I am trying to get the next row within a loop for a script i am developing... I need to display a final table row within the table that i have displayed on the page, but i only want to show it if value of the current field is not the same value of the next row. eg:
1
1657
by: charliemears | last post by:
Hi there... How can I limit search results so that there is a "Next 10" and "Previous 10"? thanks for any reply....
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.