Home AMX User Forum AMX General Discussion
Options

switch/case/wait

The following code doesnt work. It seems that the switch/case statement doesnt work inside the wait.

Strange though that if I change it to select/active it will work.
I tried assigning 'mode' to a local var but that didnt work either.
I suppose there is a perfectly valid reason why this doesnt work. I'll have to make sure I dont get caught on it next time.
define_function systemStartup (char mode)	//1=standard, 2=recorded, 3=video conf
{
	switch(mode)
	{
		case 1:{	//standard
			send_string 0,"'First switch - case = 1'";
		}
		case 2:{	//recorded
			send_string 0,"'First switch - case = 2'";
		}
		case 3:{	//video conf
			send_string 0,"'First switch - case = 3'";
		}
		default:{
			send_string 0,"'First switch - case defaulted'";
		}
	}
	
	if (systemStatus = systemIsOff)
	{
		switch(mode)
		{
			case 1:{	//standard
				send_string 0,"'2nd switch - case = 1'";
			}
			case 2:{	//recorded
				send_string 0,"'2nd switch - case = 2'";
			}
			case 3:{	//video conf
				send_string 0,"'2nd switch - case = 3'";
			}
			default:{
				send_string 0,"'2nd switch - case defaulted'";
			}
		}
		systemStatus = systemIsOn;
	}
	else
	{
		wait 30{
			switch(mode)
			{
				case 1:{	//standard
					send_string 0,"'3rd switch - case = 1'";
				}
				case 2:{	//recorded
					send_string 0,"'3rd switch - case = 2'";
				}
				case 3:{	//video conf
					send_string 0,"'3rd switch - case = 3'";
				}
				default:{
					send_string 0,"'3rd switch - case defaulted'";
				}
			}
			systemStatus = systemIsOff;
		}
	}
}
Assign Stack Ref Error - Out of Range
CHeap::Delete - ERROR(2) Attempting to delete invalide block
3rd switch - case defaulted

Comments

  • Options
    Joe HebertJoe Hebert Posts: 2,159
    filpee wrote:
    The following code doesnt work. It seems that the switch/case statement doesnt work inside the wait.

    Strange though that if I change it to select/active it will work.
    That?s affirmative, switch/case doesn't work correctly inside a wait and as you already found out select/active is the work around.
    filpee wrote:
    I suppose there is a perfectly valid reason why this doesnt work.
    There is no valid reason that I?m aware of. It's just one of the switch/case ?undocumented features.?
  • Options
    mpullinmpullin Posts: 949
    Switch/Case seems to get a bad rap around here. It's actually a very nice tool when implemented properly (unlike its NetLinx 'tarded cousin). For instance, this is a good illustration of how a switch/case should behave: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html

    Correct me if I'm wrong, but isn't Select/Active a structure exclusive to NetLinx? I've never seen it in any other language, and Google is coming up empty as well.
  • Options
    AMXJeffAMXJeff Posts: 450
    Confused, I do not think this is a switch/case issue. I think your parameter reference may have changed. Try copying the passed in parameter to a local_var, and try again.
  • Options
    viningvining Posts: 4,368
    AMX_Jeff wrote:
    Confused, I do not think this is a switch/case issue. I think your parameter reference may have changed. Try copying the passed in parameter to a local_var, and try again.
    So if the value being passed to the function was a stack var created in the code calling the function you can't use a wait as the value (stack var reference) will not exist when the wait executes. Since the wait is referencing a var that no longer exists you get the error. If you tried to do this all in the same block of code you would get a compiler error informing you that you can't use a stack var in a wait but since the wait is outside the block of code that created it the compiler can't make this evaluation but when the code runs and it tries to reference something that no longer exist you get the error.
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    AMXJeff wrote:
    Confused, I do not think this is a switch/case issue. I think your parameter reference may have changed. Try copying the passed in parameter to a local_var, and try again.
    Here is test code using a global var:
    DEFINE_DEVICE
    
    dvTP = 10001:1:0
    
    DEFINE_VARIABLE
    
    INTEGER nTest	= 1
    
    DEFINE_FUNCTION fnWaitTestSwitch() {
    
       SEND_STRING 0, 'Entering fnWaitTestSwitch'
       WAIT 10 {
       
          SWITCH (nTest) {
          
    	 CASE 1: {
    	    SEND_STRING 0, 'WaitTestCase 1'
    	 }
    
    	 CASE 2: {
    	    SEND_STRING 0, 'WaitTestCase 2'
    	 }
          }
       
       }
    
    }
    
    DEFINE_FUNCTION fnWaitTestSelelct() {
    
       SEND_STRING 0, 'Entering fnWaitTestSelelct'
       WAIT 10 {
    
          SELECT {
          
    	 ACTIVE(nTest = 1): {
    	    SEND_STRING 0, 'WaitTestActive 1'
    	 }
    
    	 ACTIVE(nTest = 2): {
    	    SEND_STRING 0, 'WaitTestActive 2'
    	 }
          }
       
       }
    
    }
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,1] {
       
       PUSH: {
          fnWaitTestSwitch()
       }
    }
    
    BUTTON_EVENT[dvTP,2] {
       
       PUSH: {
          fnWaitTestSelelct()
       }
    }
    


    Incorrect output when Button 1 is pushed:
    Line      1 :: Entering fnWaitTestSwitch - 13:04:36
    Line      2 :: Assign Stack Ref Error - Out of range - 13:04:37
    Line      3 :: SetVariable - Error 2 Tk=0x1005 - 13:04:37
    Line      4 :: DoNumberExpression - Error 2  Tk=0x2008  Line=18 - 13:04:37
    Line      5 :: GetNumber - Error 1  Tk=0x0000 - 13:04:37
    Line      6 :: DoNumberExpression - Error 2  Tk=0x2008  Line=22 - 13:04:37
    Line      7 :: GetNumber - Error 1  Tk=0x0000 - 13:04:37
    

    Correct output when Button 2 is pushed:
    Line      8 :: Entering fnWaitTestSelelct - 13:04:40
    Line      9 :: WaitTestActive 1 - 13:04:41
    
  • Options
    viningvining Posts: 4,368
    Well that pretty much makes it an issue with the switch/case not playing nice with the wait. Of course if it were a stack_var being passed that would also cause a problem and should screw up the select/active as well.
Sign In or Register to comment.