473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Check if value is defined in enum

Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.

I'd like to know if there's a clever Macro or programming method which
validates a value in an enum?

Thanks in advance,
Stanley

Nov 14 '05 #1
5 31407
DJTB wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.

I'd like to know if there's a clever Macro or programming method which
validates a value in an enum?

Thanks in advance,
Stanley


There are no facilities in the language to detect the
the number of enumerations or the values of each one.

There are no macro tricks either.

Every idea I came up with, I found a way to break that
idea.

Perhaps a better idea is to encapsulate the enum into a function
or structure. That way you could control things such as
quantity and incrementing values.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #2
On Fri, 27 Feb 2004 15:18:13 +0100, DJTB <us****@terabytemusic.cjb.net>
wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.

I'd like to know if there's a clever Macro or programming method which
validates a value in an enum?

Thanks in advance,
Stanley


As Thomas says, there's no built-in facility for that. If the largest value
is bounded, you might consider a bit-map of some type (array of char for
simplicity, actual bit-twiddling for space-efficiency...hey, an actual use
for vector<bool>! Oh darn, this is C...) where you can check a value in
constant time before using it. If the range of values it too great to make
that feasible, you may just be stuck with approaches involving b-searching,
hashing, etc., to validate values.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
DJTB wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */


It is far better to avoid this problem by not mixing enumerated values with
integer values. Use either one or the other set. Using enumerated values helps
make your code more readable; otherwise, why did you define them in the first
place? BTW, there is nothing wrong with

int test = ANOTHER_VALUE;

as long as you don't overflow test.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 14 '05 #4
DJTB wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.


This is probably the best way. If you're using gcc and you turn up your
warnings, it will even warn you about unhandled enum values in the switch.

[...]

--
Pull out a splinter to reply.
Nov 14 '05 #5
David Rubin wrote:

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */


It is far better to avoid this problem by not mixing enumerated values
with integer values. [..] here is nothing wrong with

int test = ANOTHER_VALUE;


Hi David,
It was just an example to describe the problem. I don't actually assign an
integer value in my code.

The value of the 'test' variable will be read from a socket, I've to
validate the value on the socket (== check if value is defined in enum)
before assigning it to a variable of type VALUES.

Stanley
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
3
by: Andy Venikov | last post by:
Sometimes you want to use a bitfield to hold an enum value. In such cases you would only use as many bits as are needed to encode the full set of enum values. And it is a pain to recompute and...
10
by: Patrick B | last post by:
Is there a way to check if a certain variable is an enum? Example code: public enum MyEnum { Monday, Tuesday, Wednesday } public void MyMethod()
8
by: Joe | last post by:
I have a .cs file which is linked to several other projects. All my classes in this file are defined as internal. I would like to have an enum defined as well in the namespace but I get an error...
2
by: Robert W. | last post by:
In a posting earlier this year I found a simple approach to convert a string to a particular Enum value. The one line solution looked like this: MyEnum ConvertedString = (MyEnum)...
3
by: Scott Liu | last post by:
HI, All, I have a web service doing a string search. It has an operator and a searchValue field. The operator is defined as an attribute and required. The xml is as below. <!--...
0
by: Kerry | last post by:
I have an enum defined in a web service project as follows: Public Enum Status Open = 0 Resolved = 1 Canceled = 3 End Enum I use these values in a bitmask such that any value OR'd with 1 is...
2
by: Ray Cassick \(Home\) | last post by:
I have a function that takes a value is as System.Enum and I want to be able to look at that value and determine if it is a regular enum or an enum that has a <Flag()> attribute set on it. I am...
4
by: veerleverbr | last post by:
Suppose having define an enum like this: public enum SomeEnum { Something, SomethingElse }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.