Solved

How do I add an enumeration to an array

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

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

831 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