Solved

How do I add an enumeration to an array

Posted on 2011-03-03
3
1,197 Views
Last Modified: 2012-05-11
I have the following enumeration:

Doncaster= 5
Sheffield= 14
Derby= 6
I add the enumartion to an array

Dim enumValues As Array = System.[Enum].GetValues(GetType(Enumirations.Stores))

The problem is that it need it to add into the array by order of the name of the enumaration but instead it orders it by value.  So the above example would  be  (5,6,14) but I need to stay in the same order as it is listed in the enumeration.

Thanks
0
Comment
Question by:Michael Roodt
3 Comments
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35028610

See Parameters section
http://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=VS.80).aspx

" The elements of the array are sorted by the binary values of the enumeration constants. "


You would need to use some other form of storage like Dictionary or List(Of type) if you want to make it ordered
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35028621
You could just make an integer array and store the values in that. Or make your own enumerator class that contains an int and a string and use that.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35028811
>>  but I need to stay in the same order as it is listed in the enumeration.

I don't think you're going to come across code that will do this kind of ordering. GetValues() returns the enum items in order of increasing value; GetNames() returns the enum items in lexical order. I don't see another method which would accomplish this goal.

You *may* be able to do this via Reflection, but I would ask you, "how important is this to you?" Reflection shouldn't be used willy-nilly.

I played with the following. It will sort the array in lexical order, but with the values as the data in each slot:
Dim enumValues As Array = System.Enum.GetValues(GetType(Stores))

Array.Sort(System.Enum.GetNames(GetType(Stores)), enumValues)

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now