Compiler Bug?
a_riot42
Posts: 1,624
Can anyone tell me why the compiler complains that "Case value is not a constant" when the file only contains constants?
Paul
Paul
MODULE_NAME='Switch Case Test' (***********************************************************) (* FILE CREATED ON: 07/09/2015 AT: 09:46:02 *) (***********************************************************) (***********************************************************) (***********************************************************) (* FILE_LAST_MODIFIED_ON: 07/09/2015 AT: 09:57:33 *) (***********************************************************) (* System Type : NetLinx *) (***********************************************************) (* REV HISTORY: *) (***********************************************************) (* $History: $ *) (***********************************************************) (* DEVICE NUMBER DEFINITIONS GO BELOW *) (***********************************************************) DEFINE_DEVICE (***********************************************************) (* CONSTANT DEFINITIONS GO BELOW *) (***********************************************************) DEFINE_CONSTANT cnCommand1 = 1 cnCommand2 = 2 cnCommand3 = 3 cnCommand4 = 4 cnCommand5 = 5 cnCommand6 = 6 cnCommand7 = 7 cnCommand8 = 8 cnCommand9 = 9 cnCommand10 = 10 char sCmds[10][16] = { {'command_1'}, {'command_2'}, {'command_3'}, {'command_4'}, {'command_5'}, {'command_6'}, {'command_7'}, {'command_8'}, {'command_9'}, {'command_10'} } (***********************************************************) (* DATA TYPE DEFINITIONS GO BELOW *) (***********************************************************) DEFINE_TYPE (***********************************************************) (* VARIABLE DEFINITIONS GO BELOW *) (***********************************************************) DEFINE_VARIABLE (***********************************************************) (* LATCHING DEFINITIONS GO BELOW *) (***********************************************************) DEFINE_LATCHING (***********************************************************) (* MUTUALLY EXCLUSIVE DEFINITIONS GO BELOW *) (***********************************************************) DEFINE_MUTUALLY_EXCLUSIVE (***********************************************************) (* SUBROUTINE/FUNCTION DEFINITIONS GO BELOW *) (***********************************************************) (* EXAMPLE: DEFINE_FUNCTION <RETURN_TYPE> <NAME> (<PARAMETERS>) *) (* EXAMPLE: DEFINE_CALL '<NAME>' (<PARAMETERS>) *) define_function parseResponse(char sResponse[]) { stack_var char sCommand[64] sCommand = remove_string(sResponse, "13,10", 1) switch(sCommand) { case sCmds[cnCommand1]: { // Do something } } } (***********************************************************) (* STARTUP CODE GOES BELOW *) (***********************************************************) DEFINE_START (***********************************************************) (* THE EVENTS GO BELOW *) (***********************************************************) DEFINE_EVENT (***********************************************************) (* THE ACTUAL PROGRAM GOES BELOW *) (***********************************************************) DEFINE_PROGRAM (***********************************************************) (* END OF PROGRAM *) (* DO NOT PUT ANY CODE BELOW THIS COMMENT *) (***********************************************************)
0
Comments
Maybe because it?s an array? An array of constants but it?s still an array. Don?t know for sure but that would be my guess.
This is just another example of why I almost always stick with Select Active unless the Switch Case contains a bunch of Cases with constants (as in 1, 2, 3, etc.) that are in numerical order. In my opinion, there are too many gotchas with Switch Case that makes it just not worth using. Select Active may be a bit more verbose but the flexibility and lack of any type of issues makes it worthwhile in my mind.
but - do this and it works fine.
the behavior seemed to be the same when used in the "Switch" of a switch/case or even in the "case"
You might try throwing in a stack_var and transfering the value to it and see if it works.
Paul
Then it seems to be there is no such thing as a constant array, although a string is an array of characters which can be a constant so its not consistent. I switched to if-else which should do the same thing without enraging the compiler. Strange that I hadn't come across this before.
Paul
I wonder if (just for the sheer mindlessness of it) you populated a non-arrayed stack_var just prior to the switch/case if it would work? something like
stack_var char MyString_01[16];
stack_var char MyString_02[16];
stack_var char MyString_03[16];
// etc...
then
MyString_01=sCMDs[01];
MyString_02=sCMDs[02];
MyString_03=sCMDs[03];
// etc..
then
switch(sCommand):{
case MyString_01:{
// do something
}
case MyString_02:{
// do something
}
case MyString_03:{
// do something
// etc.....
That's what I thought too. And actually, that's how I've always done it. It was just a thought to see if the whole "Using" and array of constants" could be overcome.
Interestingly, it looks like the issue here is with the current compiler not acknowledging the array is constant. Regardless of the way the way a constant array is defined (both within define_constant, or in define_variable and prefixed with 'constant') you can write compilable code that assigns a new value to specific indices. This does however throw a runtime error.