DO CASE CASE x = 1 ..... CASE x = 2 ..... CASE x = 3 ..... CASE x = 4 ..... CASE x = 5 ..... OTHERWISE ..... ENDCASEHowever you can do more with it. In the above example the order of the casing does not matter. However You can also use the ordering to rule out certain conditions.
DO CASE CASE DELETED() ... Do nothing CASE OLDVAL("MyField") == EVAL("Myfield") ... Do nothing CASE OLDVAL("MyField") > EVAL("Myfield") && Increase stock OTHERWISE && IOW, OLDVAL("MyField") < EVAL("Myfield") && Decrease stock ENDCASEIn the above the ordering is fixed. You cannot change the ordering without changing the behaviour.
DO CASE CASE THISFORM.RecordHasChanges() AND !THISFORM.SaveChanges() && Failed to save the changes CASE !SEEK(cItem,"Items") && Item does not exist CASE Items.Discontinued && Item has been dicontinued and thus cannot be ordered CASE nOrderCount = 0 CASE nOrderCount > 0 AND !oBizOrders.Order(cItem, nOrderCount) && Could not order CASE nOrderCount < 0 AND !oBizOrders.Order(cItem, nOrderCount) && Could not return order OTHERWISE && Order has succeeded ENDCASEI am well aware of that developpers comming from another language and see the DO CASE as a replacement of the switch case statement (or another simular construct in other languages) might get confused at first sight. However the last example above atually simulates a flow diagram where the CASEs are implementations of decision points.
IF !THISFORM.RecordHasChanges() OR THISFORM.SaveChanges() IF SEEK(cItem,"Items") IF !Items.Discontinued DO CASE CASE nOrderCount = 0 CASE nOrderCount > 0 IF !oBizOrders.Order(cItem, nOrderCount) && Could not be ordered ENDIF CASE nOrderCound < 0 IF !oBizOrders.Order(cItem, nOrderCount) && Could not be returned ENDIF ENDCASE ELSE && The item has been discontimued ENDIF ELSE && Item does not exist ENDIF ELSE && Item could not be saved ENDIFWhile I do recognize that nested IFs cannot always be avoided, reading through a nested if is always troublesome (at least for me). It takes me a rewrite to figure out what it exactly is supposed to do and draw a process flow diagram out of it. a DO CASE in this instance would give me a better structure to give this overview. Its a more high level of implementation a process flow IMO.
IF x IF y DO SomeThing IF z ELSE ENDIF ELSE ENDIF ELSEENDIF
DO CASE CASE !x CASE !y CASE Something() <B>AND .F.</B> && Just a trick to execute the function something uncunditionally. CASE z OTHERWISE ENDCASESo, yes, I am aware that people not used to the DO CASE usages might find it odd at first, but IMO it is not that hard to get used to it, and in the end it might prove more readable and maintainable. So yes, I think people should be aware that DO CASE is not an implementation of switch .. case but a construct that has a wider application.