Switch-case
Torbidoni
Junior Member
Hi all,
try to compile this code with Netlinx compiler and see if you get an error (like me). If yes, WHY ??????
********************************
Netlinx Studio 2 Rel.2.3 Build 2.3.0.102
Netlinx Compiler Build 2.2.0.112
********************************
Thanks,
Fabrizio.
DEFINE_EVENT
DATA_EVENT[5001:1:1]
{
STRING:
{
IF (FIND_STRING(DATA.TEXT,"$0D",1))
{STACK_VAR CHAR resp[20]
STACK_VAR INTEGER nlin
resp=REMOVE_STRING(DATA.TEXT,"$0D",1)
nlin=ATOI(resp)
SWITCH (nlin)
{
CASE 1920:
{
}(* END CASE *)
CASE 1536:
{
}(* END CASE *)
CASE 1472:
{
}(* END CASE *)
CASE 1152:
{
}(* END CASE *)
CASE 768:
{
}(* END CASE *)
CASE 704:
{
}(* END CASE *)
CASE 640:
{
}(* END CASE *)
CASE 576:
{
}(* END CASE *)
CASE 512:
{
}(* END CASE *)
CASE 448:
{
}(* END CASE *)
CASE 384:
{
}(* END CASE *)
CASE 256:
{
}(* END CASE *)
CASE 192:
{
}(* END CASE *)
CASE 128:
{
}(* END CASE *)
CASE 64:
{
}(* END CASE *)
CASE 12:
{
}(* END CASE *)
CASE 6:
{
}(* END CASE *)
CASE 5:
{
}(* END CASE *)
CASE 4:
{
}(* END CASE *)
CASE 3:
{
}(* END CASE *)
CASE 2:
{
}(* END CASE *)
CASE 1:
{
}(* END CASE *)
CASE 0:
{
}(* END CASE *)
}// end switch
}//END IF
}// END STRING
}//END DATA_EVENT
try to compile this code with Netlinx compiler and see if you get an error (like me). If yes, WHY ??????
********************************
Netlinx Studio 2 Rel.2.3 Build 2.3.0.102
Netlinx Compiler Build 2.2.0.112
********************************
Thanks,
Fabrizio.
DEFINE_EVENT
DATA_EVENT[5001:1:1]
{
STRING:
{
IF (FIND_STRING(DATA.TEXT,"$0D",1))
{STACK_VAR CHAR resp[20]
STACK_VAR INTEGER nlin
resp=REMOVE_STRING(DATA.TEXT,"$0D",1)
nlin=ATOI(resp)
SWITCH (nlin)
{
CASE 1920:
{
}(* END CASE *)
CASE 1536:
{
}(* END CASE *)
CASE 1472:
{
}(* END CASE *)
CASE 1152:
{
}(* END CASE *)
CASE 768:
{
}(* END CASE *)
CASE 704:
{
}(* END CASE *)
CASE 640:
{
}(* END CASE *)
CASE 576:
{
}(* END CASE *)
CASE 512:
{
}(* END CASE *)
CASE 448:
{
}(* END CASE *)
CASE 384:
{
}(* END CASE *)
CASE 256:
{
}(* END CASE *)
CASE 192:
{
}(* END CASE *)
CASE 128:
{
}(* END CASE *)
CASE 64:
{
}(* END CASE *)
CASE 12:
{
}(* END CASE *)
CASE 6:
{
}(* END CASE *)
CASE 5:
{
}(* END CASE *)
CASE 4:
{
}(* END CASE *)
CASE 3:
{
}(* END CASE *)
CASE 2:
{
}(* END CASE *)
CASE 1:
{
}(* END CASE *)
CASE 0:
{
}(* END CASE *)
}// end switch
}//END IF
}// END STRING
}//END DATA_EVENT
Comments
-
It appears that the compiler is only checking the lower 8 bits for each case statement. (So 1920 "appears" the same as 128, 1472 the same as 192, etc) I don't know if there's a valid reason for that, or if there's a bug, or if it's a "feature", but you might want to switch (no pun intended) over to using a SELECT/ACTIVE...
- Chip -
Torbidoni,
Yup, there is something flakey with the compiling of SWITCH CASE and I think Chip is on the right track but there is more to the story I think. If you take your original posting and reverse the CASE statements so that they are in ascending order (CASE 0: first ? CASE 1920: last) then it will compile fine.
And just to verify try compiling the next two examples. The first compiles fine, the second has issues://example 1 - this compiles fine DEFINE_FUNCTION SwitchCaseWorks (integer x) { SWITCH (x) { CASE 0: {} CASE 256: {} } } //example 2 - this complains with Duplicate Case value in Switch statement DEFINE_FUNCTION SwitchCaseNoWorkie (integer x) { SWITCH (x) { CASE 256: {} CASE 0: {} } }Something sure doesn't seem right. I usually stick with SELECT ACTIVE mainly because it?s more flexible even though it?s more verbose in some situations. -
Also, don't forget that using a SWITCH-CASE within a WAIT in a DEFINE_CALL can cause run-time errors. I was getting quite a few run-time errors and couldn't figure it out until I read technote 736. I know this has nothing to do with what you're doing - but just another reason why I use SELECT-ACTIVEs more than SWITCH-CASES. And like Joe said - you can be more precise in SELECT-ACTIVE statements.
-
Sometimes I get "duplicated case" compiler error with long (case num > 256) switch case statements. Workaround is to combine the CASE statements so that they are in ascending order (have not tried desc order though) with minimum of interruptions of the order. The latter is the crucial point. An interruption is when I have 1, 2, 3, 6, 7, 4, 5 (I have violation of order after 3, and workaround is to place statements 4 and 5 after statement 3).
Hope this helps. -
You need to declare your STACK_VARs immediately after the open curly brace ('{') in the STRING section of the DATA_EVENT. In the code you posted, the declaration comes after an IF.
HTH. -
I'm by no means certain of this, but I thought a CASE statement was limited to a CHAR variable size. But you definitely have to go in ascending order.
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums