468,497 Members | 1,847 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

convert std::string to enum

14
Hi,
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?

Thanks
rsennat
Jan 17 '08 #1
3 11022
weaknessforcats
9,207 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
{
microsoft,
ibm,
oracle
}

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
weaknessforcats
9,207 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.