Solved

How do I add an enumeration to an array

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

23 Experts available now in Live!

Get 1:1 Help Now