Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get enum count

Tags:

iphone

ipad

How to get enum count

I have a Enum

typedef enum{

DEL_TIME_VALUE    = -1,
DEL_TIMESEC_VALUE = 100,
DEL_TIMEMIN_VALUE = 200,
DEL_TIMEHOUR_VALUE = 300,
DEL_DAY_VALUE      = 1000,
DEL_COUNT_VALUE    = 1000,
....
.....
.....
DEL_END             =90002
}WORKINGTIME;

How do i get the enum count.

I try below for loop!

for(int i=DEL_TIME_VALUE; i<=DEL_END; i++) {

}

I guess its not good one!

can any one tell me how to get enum count! which are declared in enum.

Thanks in advance!

like image 391
kiran Avatar asked Feb 05 '13 21:02

kiran


2 Answers

You can't.

There is one technique that allows you to get the enum count. It looks like

typedef enum {
    value_one,
    value_two,
    value_three,
    ...
    enum_count
} my_enum;

Now the value enum_count is the count of values in the enum. However, this technique only works if the enums all carry their implicit value, where value_one is 0, value_two is 1, etc. Because of this, the last value in the enum always has the value of the count of enum values. In your case, your enum constants have explicit values that are not monotonically incrementing. There is no way to derive a count from this type of enum. And even in the theoretical world where you could derive a count, that wouldn't help you because you could not derive the value of a given enum constant.

like image 125
Lily Ballard Avatar answered Sep 18 '22 14:09

Lily Ballard


The values of an enum are compile time constants, and are not available for introspection in your code.

Instead, you can look at implementing a custom enum class with Objective-C (something akin to the Java typesafe Enum http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html).

like image 30
pgb Avatar answered Sep 20 '22 14:09

pgb