Pages: [1]   Go Down
Author Topic: enum and switch cases  (Read 1589 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps I'm not very good with google search but I cannot figure out how to use enum with switch-case. The examples I've seen (including on this forum) look like this:

Code:
enum Color { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE };

void setup()
{
  Color clr = RED;
 
  switch (clr)
  {
    case UNDEF: break;
    case RED: clr = BLUE; break;
    case BLUE: clr = YELLOW; break;   
    // etc
    default: break;
  }
}

void loop()
{
}

However these examples do not seem practical since the enum instance is always hardcoded in them.
Is there something like Enum.Parse in the arduino IDE? since code like this will throw a compiler error

at Color clr = arg:

Code:
char *arg;
arg = //*Some output from some function*;
enum Color { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE };
Color clr = arg;
...
which obviously throws the error
Code:
cannot convert "char*" to "clr" in initialization

Is there a special trick I can use similar to Enum.Parse in C#? or am I better off just using "if, else if"?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2225
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
cannot convert "char*" to "clr" in initialization

Is that really what it said?
Seems more appropriate to say the actual type mismatch:
Quote
cannot convert "char*" to "Color" in initialization

If arg was an int or char, I would suggest casting the value, but char* is pretty incompatible.

Code:
Color clr = ( Color )( unsigned int ) arg;
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If arg is of the correct type (Color), then
Code:
Color clr = arg;
is quite possible.

Something other than snippets would be good. Attempts to assign something other than a Color to clr make no sense.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@pYro_65, Yes ofcourse you are right, my mistake there.

@PaulS Well this isn't actually part of any code, it's just me thinking out loud with examples of enum switch cases from the internet. I'm not sure how complete code will help but nonetheless here is a complete sketch I wrote based on them as an example where enum instances are not hardcoded:

Code:
String content = "";
char character;
enum Color { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE };
Color clr;

void setup(){
}

void loop()
{
 
  while(Serial.available()) {
      character = Serial.read();
      content.concat(character);
  }

  if (content != "") {
    clr=content;
    switch (clr)
    {
      case UNDEF: break;
      case RED: clr = BLUE; break;
      case BLUE: clr = YELLOW; break;   
      // etc
      default: break;
    }
  }
}

In this example if it reads "RED" on the serial it changes "clr" to BLUE. That is if it compiled.

is the use of an enum switch-case pointless in anything but hardcoded use?
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your enum Color is not an array of strings, but will be a list of ints, so once it's gone through the compiler { UNDEF, RED, ORANGE, YELLOW, GREEN,  BLUE, PURPLE } becomes {0,1,2,3,4,5,6}. This obviously can't be compared with a string you are receiving via the serial port.
What you need to do is use strcmp in a series of if statements (http://www.cplusplus.com/reference/cstring/strcmp/).
Logged

Pages: [1]   Go Up
Jump to: