Page 1 of 1

Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 12:31 pm
by djrikki
I wish this DRY code was possible with the Switch-Case statement:

Code: Select all

Switch msg.id
   Case "listtree-draganddrop"	  	   		   	
   Case "listtree-sorthook"		  	   		   			  	   		   		  	   		   		
   Case "pageview-pagemode"		  	   		   			  	   		   		
   Case "group-orientation"
      layout:SaveUserData( MidStr(msg.id,0, FindStr(msg.id,"-")))
EndSwitch
Instead of the verbose:

Code: Select all

Switch msg.id
   Case "listtree-draganddrop"
      Layout:SaveUserData("listtree")
		  	   		   	
   Case "listtree-sorthook"
      Layout:SaveUserData("listtree")
		  	   		   			  	   		   		  	   		   		
   Case "pageview-pagemode"
      Layout:SaveUserData("pageview")
		  	   		   			  	   		   		
   Case "group-orientation"
      layout:SaveUserData("group")
EndSwitch

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 1:10 pm
by bitRocky
You have to use the new FallThrough()

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 1:37 pm
by djrikki
The new FallThrough() statement enables fall through it doesn't exhibit the same behaviour I am discussing. I am looking for "OR' logic.

msg.id would not contain a matching expression, just the last one it finds.

Code: Select all

      layout:SaveUserData( MidStr(msg.id,0, FindStr(msg.id,"-")))

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 2:09 pm
by jPV
Why wouldn't FallThrough work, like this?

Code: Select all

Switch msg.id
   Case "listtree-draganddrop"
      FallThrough                 
   Case "listtree-sorthook"
      FallThrough                                                                              
   Case "pageview-pagemode"                                                       
      FallThrough
   Case "group-orientation"
      layout:SaveUserData( MidStr(msg.id,0, FindStr(msg.id,"-")))
EndSwitch

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 2:16 pm
by djrikki
@JPV, the contains of the msg object will contain the properties of the final expression.

Code: Select all

   layout:SaveUserData( MidStr(msg.id,0, FindStr(msg.id,"-")))

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 2:21 pm
by jPV
djrikki wrote:@JPV, the contains of the msg object will contain the properties of the final expression.
Really? I'm pretty sure it doesn't. Why would it change there in the middle of the Switch?

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 2:36 pm
by djrikki
I will have to give it a try for real, but Fallthrough by it's name suggests inheritance is lost.

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 2:40 pm
by bitRocky

Code: Select all

Function p_Test(id)
	Switch id
		Case 1:
			FallThrough
		Case 2:
			FallThrough
		Case 3:
			FallThrough
		Case 4:
			DebugPrint("id = "..id)
	EndSwitch
EndFunction

p_Test(1)
p_Test(3)
p_Test(4)
Gives:
id = 1
id = 3
id = 4

Re: Switch...Case - multiple expressions

Posted: Sun Mar 18, 2018 3:06 pm
by djrikki
@JPV @bitRocky it appears I misunderstood the Fallthrough statement it certainly does what I want it to.

Thank you.