11 Replies Latest reply on Sep 20, 2012 1:11 PM by beverly

    How can I use Case in this context?

    synergy46

      It looks like Case can only be used in the context of another function, like IF.

       

      Question: "Does FM allow a stand alone Case statement; like the below snippet?"

        • 1. Re: How can I use Case in this context?
          sporobolus

          on 2012-09-19 15:43 synergy46 wrote

          It looks like Case can only be used in the context of another function, like IF.

           

          Question: "Does FM allow a stand alone Case statement; like the below snippet?"

           

          FileMaker doesn't have a Case script step ("statement") like the If script

          step, however a chain of If, Else If, Else script steps can do what Case does

          in other languages

           

          ("If" is the only name used for both a function and a script step in FileMaker;

          unlike script steps, functions can be used in any calculation, not just within

          a script)

          • 2. Re: How can I use Case in this context?
            wsvp

            In addition to what Steve said...

             

            The "If" "script step" is a Boolean operation, meaning it is either a positive or negative result.  0 or empty being negative, or greater than or equal to 1 being positive.  The "If' and "Case" "functions" allow for a wide variety of number, text or container results.

            • 3. Re: How can I use Case in this context?
              sporobolus

              on 2012-09-19 16:56 wsvp wrote

              In addition to what Steve said...

               

              The "If" "script step" is a Boolean operation, meaning it is either a positive or negative result.  0 or empty being negative, or greater than or equal to 1 being positive.  The "If' and "Case" "functions" allow for a wide variety of number, text or container results.

               

              i think it's much more clear to think of script steps as "statements", which do

              not have a result; they may control the flow of a script, but statements don't

              have a direct result value the way functions do

               

              what is Boolean is the way If, Case and others (as functions) treat their test

              expressions: any non-zero number (including a negative number) represents

              true, and zero represents false

              • 4. Re: How can I use Case in this context?
                beverly

                Filemaker doesn't have a SWITCH/CASE construct in script steps as found in other conditional methods. synergy46, are you trying to perform script step(s) between those "case" steps?

                 

                Can you do the math in a let() statement - pass variables that can be used later? or is that more complex than using:

                IF

                Else IF

                Else IF

                Else

                /IF

                 

                Beverly

                • 5. Re: How can I use Case in this context?
                  synergy46

                  Yes, I have been using IF... ENDIF but the resulting 'spaghetti code' would be a lot simpler to read if I could use Case... Too bad.  Maybe in FM 13? ha

                  • 6. Re: How can I use Case in this context?
                    beverly

                    Again, could you possibly do a Let() (inside a Set Variable possibly), which performs a bunch of your spaghetti inside Case(), and the result being 1 or 2 values which would then be easier to read in if/elseif/else/ script steps? I often compress a bunch of my code this way. I pre-set variables which can then be passed to fewer script steps to perform.

                     

                    Beverly

                    • 7. Re: How can I use Case in this context?
                      synergy46

                      Thank you for the thoughtful reply. 

                      The 'reality' is that my little example was just an inquiry about using Case as an independent command.  Being unable to do so results in this kind of code... doable but not the best structure.  More comments are welcome..!

                       

                      code.jpg

                      • 8. Re: How can I use Case in this context?
                        jbante

                        Structure is often dictated by the business logic of the application rather than general programming best practices. I'm not sure what problem you're seeing with the structure you're showing here that could be changed without changing the effect of running the script. I do see that there are different logic paths that set some of the same fields — sometimes to the same or different values. You could move some of the logic from the scripts If [] statements into the calculations for what to set each field to and wind up with fewer Set Field [] steps, but then you might be duplicating a lot of logic between the calculations. You could used any shared logic to set variables with boolean values and reference those variables to decide the logic paths in other calculations. Shifting the logic from script steps to calculations doesn't do much to change the structure of the code, though, in my opinion. Complicated business logic leads to complicated programming logic. There's no way around that.

                         

                        One scenario that might make sense for shifting more logic to calculations rather than the script is if the logic for some fields is simpler than the logic for others. For example, the multiple logic paths that set the Dues::Assessment_Amt to 0 could be collapsed to one logic path in the calculation for setting that field.

                        • 9. Re: How can I use Case in this context?
                          Stephen Huston

                          As jbante indicated, you can use case to evaluate the conditions within a calculated result for each SetField step, but, as there are multiple fields and variables to be set, each will still need its own calculated result.

                           

                          Sometimes the IF/ElseIf/Else options really are the simplest way to keep your code clear -- so that when you come back to it years later, you can still tell why each possible step is doing what it's doing.

                           

                          I would also comment each step to indicate what it is doing, and why, in plain language so that the logic can be deciphered easily at any time in the future.

                          • 10. Re: How can I use Case in this context?
                            DrewTenenholz

                            synergy46 --

                             

                            I agree with what I believe Beverly is trying to say.  There is every reason for you to write something like:

                             

                            Set Variable ($actionToTake ; Let ([ param1="x"; param 2="y" ; result= Case ( param1="x" AND param2="y" ; "doX" ; param1="x" OR param2="y" ; "doY" ; defaultResult ) ]; result ) // end Let )

                             

                            as a single 'controller' step

                             

                            followed by:

                             

                            If ( $actionToTake = "doX" )

                             

                               ##  do something here

                             

                            ElseIf ( $actionToTake = "doY" )

                             

                               ## do something else

                             

                            Else

                             

                              ## do something or nothing

                             

                            End If

                             

                            This can make your logic much more transparent to yourself later on, as well as having a single place to go to make changes in the desired action (followed by adding new action types and amending old ones below).

                             

                            You can even load up that first Set Variable step to set a whole collection of other $vars all at once, each of which can indicate a certain set of fields for updating.  I prefer to use a series of independent Set Variable steps for clarity, but YMMV.

                             

                            -- Drew Tenenholz

                            • 11. Re: How can I use Case in this context?
                              beverly

                              Yes, the last para by Drew was what I was thinking. I have to do similar for some of my solutions:

                               

                                   Set Variable $someVar =

                                        let (

                                           [ $a=1

                                           ; $b=3

                                           ; $c=Case

                                             ( to::x=10 and to::y=12; 120

                                             ; to::x=10 and to::y=14; 140

                                             ; to::x=10 and to::y=16; 160

                                             ; 200 // default

                                             ) // can use a case statement here & call fields

                                           ; $d="what the what?" // can use static text

                                           ; $e=Get ( CurrentDate ) // can use other functions here

                                           ; $result=$a+$b

                                           ]; $result // this what is returned to the var '$someVar', but all the other vars are SET, too!

                                        )

                               

                              Then, as Drew says, use ALL or any of the vars as needed in the IF/ELSEIF and within the script steps between them.

                               

                              And you can put Let() within a Case():

                               

                                   Case ( to::x = 1 ; Let (; 1)

                                        ; to::x = 2 ; Let (; 2)

                                        ; to::x = 3 ; Let (; 3)

                                        ; "" // default

                                        ) // the values of $a & $b will be different from the "case"

                               

                              HTH,

                              Beverly