Solved

How do I add an enumeration to an array

Posted on 2011-03-03
3
1,201 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …

820 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