Solved

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

Posted on 2004-03-24
10
2,497 Views
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?
0
Comment
Question by:FrenchJericho
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 23

Expert Comment

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

Expert Comment

by:wayside
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];

0
 

Expert Comment

by:Jarrin
ID: 10675039
Well i recomended you at use

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



0
 
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.
0
 

Expert Comment

by:Jarrin
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...  
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

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

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

Author Comment

by:FrenchJericho
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!!!!!!
0
 
LVL 14

Expert Comment

by:wayside
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.
0
 

Author Comment

by:FrenchJericho
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);
0
 
LVL 14

Accepted Solution

by:
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 {

private:
   int [] enumtoClass;
   int mapsize;
   int last

public:
  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.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now