4 Replies Latest reply on Dec 14, 2011 11:35 AM by sporobolus

    Can someone please explain!

    ian.moree

      [code]

      CUSTOM FUNCTION // http://www.fmfunctions.com/functions_display_record.php?functionId=209

      AllowInputInAutoEnter ( _self ; _value ; _triggers )

       

      by Fabrice Nordmann & Tanguy Collès

       

      Case (

      GetFieldName ( _self ) = Get ( ActiveFieldTableName ) & "::" & Get ( ActiveFieldName ) &

      Case ( Get ( ActiveRepetitionNumber ) > 1 ; "[" & Get ( ActiveRepetitionNumber ) & "]" )

      ; Get ( ActiveFieldContents )

      ; _value

      )

      [/code]

       

      COuld this have been written better? I really dont know what this is doing with all the concatenation. Just dont understand what this is doing , where

      it stops for 1st Case the ends with 2nd Case.

       

      -i

        • 1. Re: Can someone please explain!
          Mike_Mitchell

          Hello, Ian.

           

          This is basically looking at the active field and determining if it matches the name that's been passed to the custom function. Let's break it down.

           

          Case ( GetFieldName ( _self ) =

           

          This says, "If the field name of the parameter '_self' is equal to" ...  Easy enough, right?

           

          Get ( ActiveFieldTableName ) & "::" & Get ( ActiveFieldName )

           

          This says, "Fetch me the name of the active field's Table Occurrence (not table, despite what the function says), stick the double-colon on the end, then stick the name of the active field on the end." For example, you might wind up with something like "People::FirstName".

           

          Case ( Get ( ActiveRepetitionNumber ) > 1

           

          This says, "If the repetition for the active field the cursor is sitting in isn't the first repetition", then ...

           

          "[" & Get ( ActiveRepetitionNumber ) & "]"

           

          This says, take the active repetition where the cursor is sitting and stick square brackets around it. For example, you might get "[3]" if the cursor were sitting in the third repetition.

           

          So, if you had a repeating field and the cursor were sitting in the third repetition, this wouls resolve to something like: "People::FirstName[3]".

           

          Now, at this point, the second Case statement ends. Note the "&" between the "Get ( ActiveFieldName )" and the second "Case"? That means, "Attach the results of the second Case statement to my string here." If the second Case statement is false, then nothing gets attached.

           

          The first semicolon tells us what to do if the first Case condition is true. That is, if GetFieldName ( _self ) = (in our example) "People::FirstName[3]", then the result of the calculation is Get ( ActiveFieldContents ) - or whatever the field contains where the cursor is sitting.

           

          The second semicolon would be our default condition. It's what the calculation will return if all other Case conditions are not true. Since we have only one condition, it's basically a true-false test, but in this case, it will return the parameter "_value" if the cursor is NOT sitting in a field where GetFieldName ( _self ) matches (in our example) "People::FirstName[3]".

           

          Whenever I have someone else's code where there are nested Case statements like this, it's often helpful to parse them from the inside out. In this example, you might look at the second Case statement and figure out what it does, then substitute the result to figure out what the rest of the calculation does.

           

          As to your other question - could this have been written better - well, I guess that would depend on your definition of "better".         

           

          Personally, I like using the Let function to simplify calculations like this and to make them easier to read. I might, for example, create variables in a Let statement to parse the Get functions and then use the variables to run through the concatenation. (Especially where the Get functions are called more than once.) I might also create a variable for that second Case statement - calculate it in advance - and then use that instead of another Case statement for readability. But every developer is a little different; they have their own preferences.

           

          HTH

           

          Mike

          • 2. Re: Can someone please explain!
            Stephen Huston

            Hi "i" -

             

            It's the nested Case statement that makes this one less transparent that it could be.

             

            Case (

            GetFieldName ( _self ) = Get ( ActiveFieldTableName ) & "::" & Get ( ActiveFieldName ) & Case (

                 Get ( ActiveRepetitionNumber ) > 1 ; "[" & Get ( ActiveRepetitionNumber ) & "]" )

                       ; Get ( ActiveFieldContents )

                       ; _value )

             

            The concatenation appears to  deal with possible repeating fields, but it's unclear to me from the code itself whether or not the nested Case test is necessary to return  active repetition[1].

             

            Thus custom function looks like part of an auto-enter or display-only calculation. Whether or not it's overly complex depends on what value you are wanting it to return. You didn't explain what you hope to do with it.

             

            I recommend contacting the writers to see why they needed to use a nested Case. Or you could try it without the nested Case and see if it returns what you want. You will still need the concatenation if you are using this with a repeating field, otherwise, maybe not.

             

            That's all I speculate about this, based on your question and the raw function alone, without knowing what you want to accomplish or how you intend to use the results.

             

            Stephen Huston

            • 3. Re: Can someone please explain!
              ian.moree

              Well. Thank U Mike. that breaks it down awesomely for me.

               

              It is lieteral only with filemaker functions where "fieldnames" and other numbers are supposed to be. You have made me look at this so much

              clearer now.

               

               

              cheers

               

              -ian

              • 4. Re: Can someone please explain!
                sporobolus

                on 2011-12-14 11:05 ian.moree wrote

                ian.moree https://fmdev.filemaker.com/people/ian.moree created the discussion

                 

                To view the discussion, visit: https://fmdev.filemaker.com/message/64916#64916