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

How to cast an int to it's corresponding enum item?

Posted on 2004-03-24
Last Modified: 2012-05-04
I'm using an enum and also a class with public constants that were developed by two different people. Obviously, those two people didn't talk to each other and they implemented to same values using different integer values. For example, the enum looks like this:
public int WeekDaysEnum { Monday=0, Tuesday=1, Wednesday=2, Thursday=3, ...}

and the class looks like this:
public class WeekDaysClass {
     public const int Thursday=1;
     public const int Monday=5;
     public const int Wednesday=8;
     public const int Tuesday=10;

There is not direct relationship between the integer values of the items and they are listed in different orders but logically the "Monday" enum is equal to the "Monday" constant.

When I read the data from a database field, I get the integer corresponding to the class constant but I need to call a method which accepts a parameter of type WeekDaysEnum. So my question is:  how do I convert the the integer I read from the database into it's corresponding enum item? For example, how do I convert the value 5 into WeekDaysEnum.Monday?
Question by:FrenchJericho
  • 3
  • 2
  • 2
  • +2
LVL 23

Expert Comment

ID: 10673595
Simply use an int-int map.
LVL 14

Expert Comment

ID: 10673598
I don't think you can do this via a cast, you will have to write a simple mapping function, or create a Map to do the mapping, or even use an array to do the mapping.

For example, create an array of WeekDaysEnum, and fill the corresponding array location using the class constant with the enum.

For example ( this is semi-pseudo code)

WeekDaysEnum weekDaysConstantToEnum[20]; // whatever the max class constant is +1
weekDaysConstantToEnum[WeekDaysClass::Thursday] = WeekDaysEnum::Thursday;
weekDaysConstantToEnum[WeekDaysClass::Monday] = WeekDaysEnum::Monday;

Then you can just use the array to do the mapping

int day = ... // some way to get the day as a class constant
WeekDaysEnum enumday = weekDaysConstantToEnum[day];


Expert Comment

ID: 10675039
Well i recomended you at use

#define Monday         1
#define Tuesday        2
#define Wednesday    3
and so on.. just identify item.

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 30

Expert Comment

by:Mayank S
ID: 10675069
>> #define Monday         1

The source looks more like a Java code so I don't think that C/ C++ syntax would be helpful here.

Expert Comment

ID: 10675300
Very well C# looking,too =)

ok. Maybe the better choice then is use Package java.util List collection (inside Collections)
Just simple ordered list where index 1 = monday and so on...

indeed, there are many way at to do tihs ... upper mentioned Map collection, memory resident XML and so on...  
LVL 30

Expert Comment

by:Mayank S
ID: 10675382
>> Very well C# looking,too =)

Yes, could be. C# has almost the same syntax ;-)

Author Comment

ID: 10679644
Sorry I forgot to mention: it's C#.  

I thought about using a Hashtable to implement something similar to what wayside is suggesting but there are so many items that it would be a pretty big  (I used the days of the week to describe my problem in an effort to keep it simple, but my real scenario has more than 40 differnt items). I was hoping, since the integer value corresponds to the class constant value and since the constant 'name' corresponds to the enum item 'name', that there would be a way to transform the integer into the corrsponding class constant first, and then transform this constant into the corresponding enum item. I was hoping there was a way to do this using reflection maybe????

Am I day dreaming???? or is my only solution is to create this big hashtable and hard code which value corresponds to which? By the way, if I choose this approach and, at a later time, I need to do the conversion from an enum item to the class constant (which is the inverse of what I am currently trying to accomplish), does that mean that I will have to create a second big hashtable to create the inverse mapping???? You can see how this can become a nightmare to maintain if there are 40-50 items in each hashtable!!!!!!
LVL 14

Expert Comment

ID: 10680428
I don't know of any way to correlate two variables based on the variable name, or a piece of it. I think you will have to set up the mapping by hand and maintain it by hand. Sorry. :(

I think you only need one hashtable, but to go the other direction you will have to write a function to search the whole table element by element until you find a match. If you need to do this a lot you might be better off creating the second table.

It shouldn't be a big deal to set up the map, it's just pairs of values:

enumMonday, constantMonday
enumTuesday, constantTuesdays

As long as the names have something in common its easy to make sure they are correct.

Author Comment

ID: 10688416
Anybody knows of a general purpose class that will help me do that? A class capable of accepting pairs of objects (any type) and allow me to search

SearchClass objSearch = new SearchClass();
objSearch.Add(WeekDaysEnum.Monday, WeekDaysClass.Monday);
objSearch.Add(WeekDaysEnum.Tuesday, WeekDaysClass.Tuesday);
objSearch.Add(WeekDaysEnum.Sunday, WeekDaysClass.sunday);

WeekDaysEnum obj1 = (WeekDaysEnum) objSearch.Search(WeekDaysClass.Tuesday);
WeekDaysClass obj2 = (WeekDaysClass) objSearch.Search(WeekDaysEnum.Tuesday);
LVL 14

Accepted Solution

wayside earned 250 total points
ID: 10688813
It's pretty easy to write your own. Create an array of ints, add function to add and two function to search:

public class dayMap {

   int [] enumtoClass;
   int mapsize;
   int last

  dayMap(int size) {
     mapsize = size;
     enumtoClass = new int[size];
     int i;
     for (i=0; i<mapsize; i++)
            enumtoClass[i] = -1;

  void Add(enumDay, classDay) {
       enumtoClass[enumDay] = classDay;

  WeekDaysClass Search(WeekDaysEnum enumday) {
  if (enumDay > mapsize-1)
     return -1;

   return enumtoClass[enumDay];

 WeekDaysEnum Search(WeekDaysClass classday) {
 int i;
 for (i=0; i<mapsize; i++)
    if (enum.class[i] == classday)
        return i;

  return -1;

This is not pure C#, but you get the idea.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 …

840 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