473,465 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Enum and string // variable from string

Hi
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?

And also is it possible to call a variable having only string with it's
name. i.e. I have a string: "variable_name" and I want to do something like
variable_name=value;

Is it possible?

Thanks very much
Jul 23 '05 #1
8 4676
Kamil Grymuza wrote:
Hi
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?

And also is it possible to call a variable having only string with it's
name. i.e. I have a string: "variable_name" and I want to do something like
variable_name=value;

Is it possible?

Thanks very much


A nice design is to have your enum as a class.
Give the class some methods for convert from
string to value and vice-versa.

By having the enum as a type, you could add more
control to the assignments.

Otherwise, try either a table of <string, value>
or a std::map.

--
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.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #2
Kamil Grymuza wrote:
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?
#define name_of(x) #x
....
cout << name_of(ID1) << endl;
And also is it possible to call a variable having only string with it's
name. i.e. I have a string: "variable_name" and I want to do something like
variable_name=value;
I don't understand the problem you're trying to solve.
Is it possible?


As soon as you tell what "it" is.

V
Jul 23 '05 #3
Kamil Grymuza wrote:
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?


enum ID {
ID1, ID2
};

std::ostream & operator << (std::ostream & os, ID id)
{
switch (id)
{
case ID1: os << "ID1"; break;
case ID2: os << "ID2"; break;
}
return os;
}

int main ()
{
std::cout << ID1 << std::endl << ID2 << std::endl;
}
--
Salu2
Jul 23 '05 #4
The problem is that I have some variables I can't put in an array, they
are names of buttons, text controls etc. I obtain a name of element that
called a function and by that name I would like to call some method
of this object, and I don't know whether this is possible by some simple
cast from string (name of the variable) to some pointer or whatever, the
other idea I have is to make a switch and compare all the possibilities
but this is totally unelegant.

If it is possible to make that 'cast' could you please write me how to
do it, or maybe you have some idea how to solve the problem?

Thanks for your help
bye
Jul 23 '05 #5
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?
Bye
Jul 23 '05 #6
I have done something like the following sometimes ( I haven't compiled
this, it is just from memory.. )
create a file that invokes a macro on each element, and do too files that
defines the macro differently.

Hope this is what you want...
----file: enum.inc
ITEM(ID1),
ITEM(ID2),
ITEM(ID3),
ITEM(ID4)

----file: enum_names.h
#define ITEM(x) #x
const char * names[40] = {
#include "enum.inc"
};
#undef ITEM

----file: enum_values.h
#define ITEM(x) x
enum {
#include "enum.inc"
};
#undef ITEM

--
Jesper Madsen, SAXoTECH

"The truth of the matter is that you always know the right thing to do. The
hard part is doing it"
- General H. Norman Schwartzoff
"Kamil Grymuza" <gr****@wp.pl> wrote in message
news:d6**********@nemesis.news.tpi.pl...
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?
Bye

Jul 23 '05 #7
Kamil Grymuza wrote:
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?


Not really. If you don't have duplicate IDs, then you can use Thomas'
suggestion about a 'map<ID,string>'. If you have duplicates, you can't
use that, for obvious reasons.

Whatever other thing you wrote about "names of buttons, text controls
etc.", I have absolutely no idea what you need. You're apparently trying
to solve some problem by "casting" something "from string" or vice versa,
but you didn't say what that problem was. Have you tried simply taking
the address of the variable, converting it to void* and displaying it?
Again, I have no idea if that's what you need because I don't understand
the problem you're trying to solve.

You could, of course, create a 'map<void*,string>' and then add "names"
when you create your objects, and then display those names based on the
address of the object when you feel like it. That's intrusive (requires
you to change code), and not really reliable since you can simply assign
the same "name" to different object addresses and it's not going to stop
you... Maybe that's what you need?

V
Jul 23 '05 #8


--
Jesper Madsen, SAXoTECH

"The truth of the matter is that you always know the right thing to do. The
hard part is doing it"
- General H. Norman Schwartzoff
"Jesper Madsen" <ba***@mail.stofanet.dk> wrote in message
news:42*********************@nntp04.dk.telia.net.. .
I have done something like the following sometimes ( I haven't compiled
this, it is just from memory.. )
create a file that invokes a macro on each element, and do too files that
defines the macro differently.

Hope this is what you want...
----file: enum.inc
ITEM(ID1),
ITEM(ID2),
ITEM(ID3),
ITEM(ID4)

----file: enum_names.h
#define ITEM(x) #x
const char * names[40] = {
#include "enum.inc"
};
#undef ITEM

----file: enum_values.h
#define ITEM(x) x
enum {
#include "enum.inc"
};
#undef ITEM

forgot to write: std::cout << "name: " << names[ event.GetID() ] << " value
:" << event.GetID() << std::endl;


--
Jesper Madsen, SAXoTECH

"The truth of the matter is that you always know the right thing to do. The hard part is doing it"
- General H. Norman Schwartzoff
"Kamil Grymuza" <gr****@wp.pl> wrote in message
news:d6**********@nemesis.news.tpi.pl...
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?
Bye


Jul 23 '05 #9

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

Similar topics

2
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
10
by: Ken Allen | last post by:
The ToString() function, when applied to a variable that is an enumeration type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but...
4
by: marc.gibian | last post by:
I have been trying to improve the quality of my C# and ADO.NET coding. One of the books I've read strongly advises against using string values to address individual values in DataRow objects. This...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
6
by: Brian Haynes | last post by:
I've read all the posts in this forum that I can find that look related to this issue and I have only found 1 solution that I consider to be a bit of a hack. What I want to do is assign a value to...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
4
by: veerleverbr | last post by:
Suppose having define an enum like this: public enum SomeEnum { Something, SomethingElse }
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.