Convert wxString to enum. Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
scriptdaemon
Experienced Solver
Experienced Solver
Posts: 93
Joined: Sun Mar 15, 2009 10:28 pm

Convert wxString to enum.

Post by scriptdaemon » Sat Jun 27, 2009 5:42 am

I have some enum values:

Code: Select all

enum BGStyle
{
    Stretched,
    Centered,
    Tiled
};
I would like to convert a wxString such as "Stretched" to the corresponding enum value. Is there an easy way to do this without using if/else statements?

vdell
Moderator
Moderator
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Re: Convert wxString to enum.

Post by vdell » Sat Jun 27, 2009 6:49 am

scriptdaemon wrote:Is there an easy way to do this without using if/else statements?
Nope. You could code something similar to EnumBinder if you don't want to use the if/else-solution.

HTH
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Sat Jun 27, 2009 3:35 pm

Java does this, but I don't know any similar facility in C++. It should be quite straightforward to implement it in a generic way if that's what you want though
"Keyboard not detected. Press F1 to continue"
-- Windows

ninja9578
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Thu Jan 29, 2009 3:33 pm

Post by ninja9578 » Sat Jun 27, 2009 4:11 pm

C++ doesn't have a way to do that but it's fairly easy

Code: Select all

switch(yourstring[0]){
   case 'S':
      assert(yourstring == "Stretched");
      return Stretched;
   case 'C':
      assert(yourstring == "Centred");
      return Centred;
   case 'T':
      assert(yourstring == "Tiled");
      return Tiled;
};
Just make sure that wxStrings guarantees that you have at least one character ('\0') in it. std::string does, but I'm not sure about wxString. Seems like it would though.

scriptdaemon
Experienced Solver
Experienced Solver
Posts: 93
Joined: Sun Mar 15, 2009 10:28 pm

Post by scriptdaemon » Sun Jun 28, 2009 7:10 am

ninja9578 wrote:C++ doesn't have a way to do that but it's fairly easy

Code: Select all

switch(yourstring[0]){
   case 'S':
      assert(yourstring == "Stretched");
      return Stretched;
   case 'C':
      assert(yourstring == "Centred");
      return Centred;
   case 'T':
      assert(yourstring == "Tiled");
      return Tiled;
};
Just make sure that wxStrings guarantees that you have at least one character ('\0') in it. std::string does, but I'm not sure about wxString. Seems like it would though.
This would be the easiest, but the EnumBinder might have some use later on.

Post Reply