Switch (n) in a Define_Call?
PyroGuy
Posts: 121
Hi all...
Banging my head against something obvious maybe? (most likely)
I can not get the following to execute. It should be just a little routine to trigger page flips on a bunch of KPs.
It is inside of a sub-routine and everything else inside the CALL runs.
Does a SWITCH not hold up in a CALL? I would use a GET_LAST but I need to trigger all KPs to page flip except for the one that is triggering this CALL.
I thought this would be the easiest way - skiproom is the varible of the room I don't want to page flip,
This is a cut from inside the routine. The wait is to give some other things before a chance to catch up.
WAIT 4
{
FOR(nCount = 1; nCount <= 11; nCount ++)
{
IF(nCount<>SkipRoom)
{
Switch (nCount)
{
Case 1:
{
SEND_COMMAND dvDMS1, " 'PAGE-IntercomReply' "
}
Case 2:
{
SEND_COMMAND dvDMS2, " 'PAGE-IntercomReply' "
}
// etc down to dvDMS11
}
}
}
}
Banging my head against something obvious maybe? (most likely)
I can not get the following to execute. It should be just a little routine to trigger page flips on a bunch of KPs.
It is inside of a sub-routine and everything else inside the CALL runs.
Does a SWITCH not hold up in a CALL? I would use a GET_LAST but I need to trigger all KPs to page flip except for the one that is triggering this CALL.
I thought this would be the easiest way - skiproom is the varible of the room I don't want to page flip,
This is a cut from inside the routine. The wait is to give some other things before a chance to catch up.
WAIT 4
{
FOR(nCount = 1; nCount <= 11; nCount ++)
{
IF(nCount<>SkipRoom)
{
Switch (nCount)
{
Case 1:
{
SEND_COMMAND dvDMS1, " 'PAGE-IntercomReply' "
}
Case 2:
{
SEND_COMMAND dvDMS2, " 'PAGE-IntercomReply' "
}
// etc down to dvDMS11
}
}
}
}
0
Comments
It's a bug that's been documented before (I'll post the link if I can find it) and I guess it hasn't been fixed yet.
Try the same code but use SELECT ACTIVE instead.
So, if SkipRoom is a variable - couldn't it change by the time the WAIT expires?
I'm going to give Select Active a shot. I was actually considered hard coding a dozen IF statements this was driving me nuts.
Thanks
Well . . . since it's still a variable and it is possible that another keypad can reinitiate the CALL, however unlikely, I'd highly recommend the good practice of evaluating then wait. I wouldn't care if it was 1/10 of a second delay of the evaluation - it's a good habit to get into when you have WAITs and you're evaluating.
I'd highly suggest you do something like this:
Forgetting about the Wait for a minute, and also forgetting that SkipRoom MAY change before wait execution, Why not "clean up" the code a bit? Create a "_DMS_Device_Array_", and do something like:
WAIT 4
{
FOR(nCount = 1; nCount <= 11; nCount ++)
{
IF(nCount<>SkipRoom)
{
SEND_COMMAND _DMS_Device_Array_[nCount], " 'PAGE-IntercomReply' "
}
}
}
No Need For a SWITCH/CASE or SELECT/ACTIVE or IF/IF/IF....ect...
Why would each DMS need its own wait? Isn't the wait for "other things"?
Wife is looking at me nastily cuz we're late to a birthday party . . . . could someone please explain what I mean a bit more in depth?
I?m here to promote the existence of the SWITCH CASE inside a WAIT bug.
Anything else I say might be considered a conflict of interest.
Me neither...Its too early to activate my mind reading helmet...
I Keed.....I Keed!!!!!
So you guys are discussing two different pieces of code.
Joe Hebert wrote: I think in the past to get around this I would have the wait execute a function call which contained the switch case and other time I probably just gave up with out realizing the cause of the problem and did a select active as you previously suggested. I also find that the switch case in general has a lot of quirky behavior problems which cause me to favor select active which always seems to function as advertised.
What's that other switch case problem? A single char case at the begining of the switch that used to make me bang my head against the wall.
Basically what I'm saying is that if you wait and then evaluate, you run the risk of evaluating the wrong value. Where as if you evaluate and then wait, you'll always get the result you want. I don't care about SWITCH...CASE or SELECT...ACTIVE, it's the WAIT 4 that bothers me.
What if the function is called 3 times in a row, one right after another? By the time the WAIT expires and you evaluate the value, the first two calls won't activate, but the last will.
So - I'm promoting evaluation before waits. I don't care which way you evaluate it.
The Select Active approach didn't work (well it did - but not the way I wanted it to) - code often does that! I want a "complile_as_desired" function to go along with the regular "complile_as_written".
I like the dev array suggestion and may put it in a future revision. Since this had to get out to the client quickly, I realized the issue was with the wait and looked further at the code.
The wait 1,2,3, and 4 were just to allow 'other things' to get done so as a wise programmer once told me, keep it simple. It put the FOR loop as the first thing to execute in the CALL, no wait required, and everything else happens after that. It really doesn't matter WHEN the KP flip happens, before or after the switcher settings, so call me a chicken, but I just avoided the problem by dodging it.
Thanks to all for the suggestions - the info is good to add to my arsenal.
Cheers & Thanks!
Just for the record, here?s a thread from a little over a year ago with posted code that proves that SWITCH CASE in a WAIT doesn?t work and that it generates run time errors. Same code with SELECT ACTIVE works fine.
http://amxforums.com/showthread.php?t=3821
Here is a different type of scenario that shows another quirk with SWITCH CASE:
This example compiles fine:
This example, strange as it may seem, does not compile. It generates the error: Duplicate CASE values in SWITCH statement. Say what? The error doesn?t say say what, I said that.
If you drive down the SWITCH CASE road you must be cautious of the pot holes.
I have seen this error for unknown reasons! I didn't realize there was some (crazy) logic behind it.
What are the conditions for this? A multiple of the first case? Only 256? I use switch/case ALL the time and only rarely have problems.
I'd like to know a bit more about this one so I can swerve around it
I wonder if switch()..case is treating the case values as 8 bit integers? Have you tried a case 1: and a case 257: to see if the same problem exists?
Jeff
Indeed,
throws the same error.
25701 ? 101 = 25600
25600/256 = 100 = error
Weird eh?
Seems like it's a case of the Switch() only comparing the first 8 bits. Maybe tech support could talk to engineering and get a better understanding of how things work under the hood. With that understanding, it should be easier to formulate a list of limitations for switch()..case statements.
Jeff
Since I have not experimented with this at all, I am wondering: It compiles without problems, but does it run without problems?
Jeff
Thanks Joe, my faith in Switch() has been restored. Next time I am talking with tech support, I will try to remember to mention this and see if they can fix the compiler.
Jeff
I never said exactly how much faith I have in switch()..case, just that it is restored I really only use switch()..case on a very limited basis. If I am dealing with more than 5 or 10 possibilities, I try to look at what I am asking it to do and see if there is a way to break it down further or approach it from a different angle.
On occasion tho, I have used a switch()..case with values over 10, but not higher than 250.
Jeff
And naturally (!) if case 256: is moved before case 0:, or case 25601: is moved before case 1:, it will not compile.
Spire_Jeff, I believe you are correct. ...at compiler.
I am curious of the other problems with switch/case. I know the limitation of not being able to fall through to lower cases (implied break), and I am now reminded of not being able to use switch/case in a wait...what other problems are there?
Won't work
Will work
Jeff