0

In C# use code:

enum COMPUTER_NAME_FORMAT
{
        ComputerNameNetBIOS,
        ComputerNameDnsHostname,
        ComputerNameDnsDomain,
        ComputerNameDnsFullyQualified,
        ComputerNamePhysicalNetBIOS,
        ComputerNamePhysicalDnsHostname,
        ComputerNamePhysicalDnsDomain,
        ComputerNamePhysicalDnsFullyQualified
}

string format = "ComputerNameDnsFullyQualified";
(COMPUTER_NAME_FORMAT)Enum.Parse(typeof(COMPUTER_NAME_FORMAT), format)

How use this in C++ ?

Dmitry
  • 13,797
  • 6
  • 32
  • 48
  • 1
    You can use a macro to generate your enum, an array of its values, and array of its strings side-by-side, and then write an initialization function that will walk the string array to find your string, then return the corresponding value from the values array. You could a map to speed that up. I couldn't find a solution on SO, but there are solutions in the other direction that can help you get started: http://stackoverflow.com/questions/201593/is-there-a-simple-script-to-convert-c-enum-to-string I've also written a simple library that does this https://github.com/aantron/better-enums – antron Jun 20 '15 at 11:18

1 Answers1

1

There is no such function in C or C++. But you can make a std::map<std::string, COMPUTER_NAME_FORMAT> m that does that.

Fill the map by doing m["ComputerNameNetBIOS"] = ComputerNameNetBIOS; etc. Then use auto f = m.find(format); if (f != m.end()) { ... value is in f.second ... }

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227