Switch...Case - multiple expressions

Feature requests for future versions of Hollywood can be voiced here
Post Reply
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Switch...Case - multiple expressions

Post 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
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

bitRocky
Posts: 112
Joined: Fri Nov 14, 2014 6:01 pm

Re: Switch...Case - multiple expressions

Post by bitRocky »

You have to use the new FallThrough()

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: Switch...Case - multiple expressions

Post 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,"-")))
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

User avatar
jPV
Posts: 365
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Switch...Case - multiple expressions

Post 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

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: Switch...Case - multiple expressions

Post 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,"-")))
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

User avatar
jPV
Posts: 365
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Switch...Case - multiple expressions

Post 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?

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: Switch...Case - multiple expressions

Post by djrikki »

I will have to give it a try for real, but Fallthrough by it's name suggests inheritance is lost.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

bitRocky
Posts: 112
Joined: Fri Nov 14, 2014 6:01 pm

Re: Switch...Case - multiple expressions

Post 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

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: Switch...Case - multiple expressions

Post by djrikki »

@JPV @bitRocky it appears I misunderstood the Fallthrough statement it certainly does what I want it to.

Thank you.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

Post Reply