Solved

How do I add an enumeration to an array

Posted on 2011-03-03
3
1,206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
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 …
Introduction to Processes

628 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