470,871 Members | 1,347 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,871 developers. It's quick & easy.

convert std::string to enum

Converting enum to strings is simple. Just we can use the enum as the i ndex to get the string from an array of strings.

But how do I convert string to enum. Should I need to compare with every string in the array and return the enum when it matches.

Whats the efficient way to do this?

Jan 17 '08 #1
3 11219
9,208 Expert Mod 8TB
Nope. An enum is a named integer value. To return BLUE you would have to compare to "BLUE".
Jan 17 '08 #2
i have an enum called

enum companies

to get the corresponding integer enum value :--

int int_value = (int)Enum.Parse(typeof(companies), "microsoft", true);

to convert integer enum value to its corresponding string :--

object s =Enum.Parse(typeof(companies), Convert.ToString(int_value));
string st = Convert.ToString(s);
Jan 21 '08 #3
9,208 Expert Mod 8TB
Your example is Microsoft Managed C++. My reply was ANSI C++.
Jan 21 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Karthik | last post: by
16 posts views Thread by Khuong Dinh Pham | last post: by
10 posts views Thread by sposes | last post: by
2 posts views Thread by Abhishek | last post: by
84 posts views Thread by Peter Olcott | last post: by
11 posts views Thread by Jacek Dziedzic | last post: by
11 posts views Thread by Sudzzz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.