473,321 Members | 1,667 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,321 software developers and data experts.

how to access to enumeration member via it's number

Hello,
I have to following issue.
A type is declared based on an enum:
"
enum TpAddressPlan {
P_ADDRESS_PLAN_NOT_PRESENT = 0,
P_ADDRESS_PLAN_UNDEFINED = 1,
P_ADDRESS_PLAN_IP = 2,
P_ADDRESS_PLAN_MULTICAST = 3,
P_ADDRESS_PLAN_UNICAST = 4,
P_ADDRESS_PLAN_E164 = 5,
P_ADDRESS_PLAN_AESA = 6,
P_ADDRESS_PLAN_URL = 7,
P_ADDRESS_PLAN_NSAP = 8,
P_ADDRESS_PLAN_SMTP = 9,
P_ADDRESS_PLAN_MSMAIL = 10,
P_ADDRESS_PLAN_X400 = 11,
P_ADDRESS_PLAN_SIP = 12,
P_ADDRESS_PLAN_ANY = 13,
P_ADDRESS_PLAN_MIN = 14
};
typedef enum TpAddressPlan TpAddressPlan;
"

And a function receive an integer corresponding to the enumeration
member that I must convert into the member to fill a structure member
of the type TpAddressPlan:
"
this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Pla n =
AddressPlan; // [0-14]
"

How can I assign to AddressPlan the enumeration value, starting from
it's integer value: kind of

???AddressPlan = TpAddressPlan (i)???

Thanks for your help!

Feb 20 '07 #1
10 2052
On Feb 20, 2:18 pm, brarat...@gmail.com wrote:
Hello,
I have to following issue.
A type is declared based on an enum:
"
enum TpAddressPlan {
P_ADDRESS_PLAN_NOT_PRESENT = 0,
P_ADDRESS_PLAN_UNDEFINED = 1,
P_ADDRESS_PLAN_IP = 2,
P_ADDRESS_PLAN_MULTICAST = 3,
P_ADDRESS_PLAN_UNICAST = 4,
P_ADDRESS_PLAN_E164 = 5,
P_ADDRESS_PLAN_AESA = 6,
P_ADDRESS_PLAN_URL = 7,
P_ADDRESS_PLAN_NSAP = 8,
P_ADDRESS_PLAN_SMTP = 9,
P_ADDRESS_PLAN_MSMAIL = 10,
P_ADDRESS_PLAN_X400 = 11,
P_ADDRESS_PLAN_SIP = 12,
P_ADDRESS_PLAN_ANY = 13,
P_ADDRESS_PLAN_MIN = 14};

typedef enum TpAddressPlan TpAddressPlan;
You don't need a typedef in C++.

And a function receive an integer corresponding to the enumeration
member that I must convert into the member to fill a structure member
of the type TpAddressPlan:
"
this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Pla n =
AddressPlan; // [0-14]
"

How can I assign to AddressPlan the enumeration value, starting from
it's integer value: kind of

???AddressPlan = TpAddressPlan (i)???
If you are sure that i is in the correct range (0-14) and the value
corresponds to the right enumeration then just use

AddressPlan = TpAddressPlan(i);

--
Erik Wikström

Feb 20 '07 #2
br*******@gmail.com wrote:
I have to following issue.
A type is declared based on an enum:
"
enum TpAddressPlan {
P_ADDRESS_PLAN_NOT_PRESENT = 0,
P_ADDRESS_PLAN_UNDEFINED = 1,
P_ADDRESS_PLAN_IP = 2,
P_ADDRESS_PLAN_MULTICAST = 3,
P_ADDRESS_PLAN_UNICAST = 4,
P_ADDRESS_PLAN_E164 = 5,
P_ADDRESS_PLAN_AESA = 6,
P_ADDRESS_PLAN_URL = 7,
P_ADDRESS_PLAN_NSAP = 8,
P_ADDRESS_PLAN_SMTP = 9,
P_ADDRESS_PLAN_MSMAIL = 10,
P_ADDRESS_PLAN_X400 = 11,
P_ADDRESS_PLAN_SIP = 12,
P_ADDRESS_PLAN_ANY = 13,
P_ADDRESS_PLAN_MIN = 14
};
typedef enum TpAddressPlan TpAddressPlan;
That's unnecessary. 'enum' in variable declarations is optional.
"

And a function receive an integer corresponding to the enumeration
member that I must convert into the member to fill a structure member
of the type TpAddressPlan:
"
this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Pla n =
AddressPlan; // [0-14]
"

How can I assign to AddressPlan the enumeration value, starting from
it's integer value: kind of

???AddressPlan = TpAddressPlan (i)???
That's exactly how you do it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 20 '07 #3


Thanks a lot!!! all.

I wrote "AddressPlan = TpAddressPlan[i]"!! That was the issue!
Anyway, I didn't knew that "AddressPlan = TpAddressPlan(i)" would
work.

Thanks a lot!

Feb 20 '07 #4
On 20 Feb 2007 05:42:55 -0800, "Erik Wikström" <er****@student.chalmers.se>
wrote:
>On Feb 20, 2:18 pm, brarat...@gmail.com wrote:
>Hello,
I have to following issue.
A type is declared based on an enum:
"
enum TpAddressPlan {
P_ADDRESS_PLAN_NOT_PRESENT = 0,
P_ADDRESS_PLAN_UNDEFINED = 1,
P_ADDRESS_PLAN_IP = 2,
P_ADDRESS_PLAN_MULTICAST = 3,
P_ADDRESS_PLAN_UNICAST = 4,
P_ADDRESS_PLAN_E164 = 5,
P_ADDRESS_PLAN_AESA = 6,
P_ADDRESS_PLAN_URL = 7,
P_ADDRESS_PLAN_NSAP = 8,
P_ADDRESS_PLAN_SMTP = 9,
P_ADDRESS_PLAN_MSMAIL = 10,
P_ADDRESS_PLAN_X400 = 11,
P_ADDRESS_PLAN_SIP = 12,
P_ADDRESS_PLAN_ANY = 13,
P_ADDRESS_PLAN_MIN = 14};

typedef enum TpAddressPlan TpAddressPlan;

You don't need a typedef in C++.

>And a function receive an integer corresponding to the enumeration
member that I must convert into the member to fill a structure member
of the type TpAddressPlan:
"
this->msg.CEType_u.msgreq.CERequest_u.qryreq.userID.Pla n =
AddressPlan; // [0-14]
"

How can I assign to AddressPlan the enumeration value, starting from
it's integer value: kind of

???AddressPlan = TpAddressPlan (i)???

If you are sure that i is in the correct range (0-14) and the value
corresponds to the right enumeration then just use

AddressPlan = TpAddressPlan(i);
I've never seen this expression form before. Where is it specified in the
standard?

-dr
Feb 20 '07 #5
Dave Rahardja wrote:
On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
<er****@student.chalmers.sewrote:
>[...]
If you are sure that i is in the correct range (0-14) and the value
corresponds to the right enumeration then just use

AddressPlan = TpAddressPlan(i);

I've never seen this expression form before. Where is it specified in
the standard?
5.2.3

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 20 '07 #6
On Tue, 20 Feb 2007 09:39:43 -0500, "Victor Bazarov" <v.********@comAcast.net>
wrote:
>Dave Rahardja wrote:
>On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
<er****@student.chalmers.sewrote:
>>[...]
If you are sure that i is in the correct range (0-14) and the value
corresponds to the right enumeration then just use

AddressPlan = TpAddressPlan(i);

I've never seen this expression form before. Where is it specified in
the standard?

5.2.3
Of course. Somehow I misunderstood what the OP meant. I thought there was a
notation whereby you can retrieve the n'th constant in an enumeration, e.g.

enum E
{
FOO = 2,
BAR = 4,
BAZ = 9
};

E e = E[1]; // e == 4

Which is ill-formed.

-dr
Feb 22 '07 #7
Dave Rahardja wrote:
On Tue, 20 Feb 2007 09:39:43 -0500, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Dave Rahardja wrote:
>>On 20 Feb 2007 05:42:55 -0800, "Erik Wikström"
<er****@student.chalmers.sewrote:
[...]
If you are sure that i is in the correct range (0-14) and the value
corresponds to the right enumeration then just use

AddressPlan = TpAddressPlan(i);

I've never seen this expression form before. Where is it specified
in the standard?

5.2.3

Of course. Somehow I misunderstood what the OP meant. I thought there
was a notation whereby you can retrieve the n'th constant in an
enumeration, e.g.

enum E
{
FOO = 2,
BAR = 4,
BAZ = 9
};

E e = E[1]; // e == 4

Which is ill-formed.
Well, right, there is no way. That's why there are several "smart
enum" implementations out there. Essentially, what's recommended is
to implement your own type with conversions to 'int' (if you need
those) and with named constants of that type. If you do, you have
the chance to define your own operator[] which would give you the
indexed value. OTOH, using indexing with enumerations does not
actually follow the ideology behind enumerations. What you have
here is not an enumeration (named constants) but an array of some
values which also have tags. You should perhaps consider

const int somearray[] = { 2, 4, 9 };
const int& FOO = somearray[0];
const int& BAR = somearray[1];
const int& BAZ = somearray[2];

(or even wrapping it into a struct named 'E'...)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 22 '07 #8
On Thu, 22 Feb 2007 09:27:57 -0500, "Victor Bazarov" <v.********@comAcast.net>
wrote:
>Of course. Somehow I misunderstood what the OP meant. I thought there
was a notation whereby you can retrieve the n'th constant in an
enumeration, e.g.

enum E
{
FOO = 2,
BAR = 4,
BAZ = 9
};

E e = E[1]; // e == 4

Which is ill-formed.

Well, right, there is no way. That's why there are several "smart
enum" implementations out there. Essentially, what's recommended is
to implement your own type with conversions to 'int' (if you need
those) and with named constants of that type. If you do, you have
the chance to define your own operator[] which would give you the
indexed value. OTOH, using indexing with enumerations does not
actually follow the ideology behind enumerations. What you have
here is not an enumeration (named constants) but an array of some
values which also have tags. You should perhaps consider

const int somearray[] = { 2, 4, 9 };
const int& FOO = somearray[0];
const int& BAR = somearray[1];
const int& BAZ = somearray[2];

(or even wrapping it into a struct named 'E'...)
Yes, but with one difference: an indexed enumeration would be strictly a
compile-time construct. You can't take its address, and you can't use a
run-time variable as the index.

I smell a template metaprogram coming on...

-dr
Feb 23 '07 #9
Dave Rahardja wrote:
On Thu, 22 Feb 2007 09:27:57 -0500, "Victor Bazarov"
<v.********@comAcast.netwrote:
>>Of course. Somehow I misunderstood what the OP meant. I thought
there was a notation whereby you can retrieve the n'th constant in
an enumeration, e.g.

enum E
{
FOO = 2,
BAR = 4,
BAZ = 9
};

E e = E[1]; // e == 4

Which is ill-formed.

Well, right, there is no way. That's why there are several "smart
enum" implementations out there. Essentially, what's recommended is
to implement your own type with conversions to 'int' (if you need
those) and with named constants of that type. If you do, you have
the chance to define your own operator[] which would give you the
indexed value. OTOH, using indexing with enumerations does not
actually follow the ideology behind enumerations. What you have
here is not an enumeration (named constants) but an array of some
values which also have tags. You should perhaps consider

const int somearray[] = { 2, 4, 9 };
const int& FOO = somearray[0];
const int& BAR = somearray[1];
const int& BAZ = somearray[2];

(or even wrapping it into a struct named 'E'...)

Yes, but with one difference: an indexed enumeration would be
strictly a compile-time construct. You can't take its address, and
you can't use a run-time variable as the index.

I smell a template metaprogram coming on...
It might be an interesting exercise, but I honestly don't see any
practical value in it. Do you?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 23 '07 #10
On Fri, 23 Feb 2007 08:21:18 -0500, "Victor Bazarov" <v.********@comAcast.net>
wrote:
>Yes, but with one difference: an indexed enumeration would be
strictly a compile-time construct. You can't take its address, and
you can't use a run-time variable as the index.

I smell a template metaprogram coming on...

It might be an interesting exercise, but I honestly don't see any
practical value in it. Do you?
Sure, as a lookup table at compile time. Come to think of it, it's much easier
to use boost::mpl::vector_c<int, ...to achieve this.

-dr
Feb 23 '07 #11

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

Similar topics

4
by: Charles Law | last post by:
When displaying the Color Picker for a web page (in the VS IDE), Background is listed on the System Colors tab. Also, Color.FromName("Background").IsNamedColor returns True. However,...
2
by: PaulUK | last post by:
When I use the following C# code: string colorNames = Enum.GetNames(typeof(KnownColor)); colorNames does not contain the full list of member names detailed in the System.Drawing.KnownColor...
5
by: Neil Zanella | last post by:
Hello, I have seen code that does the following: enum Letter { X, Y, Z, numLetters }; Is this considered good or bad code? On one hand, the code seems more maintainable because no matter...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Brett Kelly | last post by:
Hello all, I'm in a situation where I need to retrieve a member from the System.Data.SqlDbType enumeration knowing only the type name. At this point, I'm just trying to get reflection to...
13
by: Peter Chant | last post by:
I'm considering setting a website up for a club. I do not plan the contents to be for public consumption, but on the other hand I'm not going to have anything on there that is confidential, that...
1
by: Per Rollvang | last post by:
Hi All! I am creating a usercontrol, and this control have some text that can be placed on the control via the enumeration "ContentAlignment". I will, however, give the user a chance to put the...
8
by: rcdailey | last post by:
Will there be a feature in C++0x to facilitate getting the number of items in an enumeration? For example, in C++03, you have to do something like this: enum Foo { FOO1 = 0, FOO2, FOO3,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.