1 2 Previous Next 20 Replies Latest reply on Feb 11, 2012 2:04 PM by BruceHerbach

    Variable Type Casts ?

    gdurniak

      I set $Year = 2012 ( in various ways, in various places )

       

      Then check If $Year > 0, and it fails

       

      I then try GetasNumber ( $Year ) > 0, and it works

       

       

      It appears that $Year end up as Text.

       

      Are the rules for Variable Type Casting documented anywhere?

       

      Did this change at all in FileMaker 11?

       

      The script in question worked in FileMaker 9

       

      Thanks,

      greg

        • 1. Re: Variable Type Casts ?
          comment

          gdurniak wrote:

           

          I set $Year = 2012  ( in various ways, in various places )

           

          Then check  If $Year > 0,  and it fails

           

          Could you post a file showing this?

          • 2. Re: Variable Type Casts ?
            TonyWhite

            Works here in FileMaker Pro Advanced 11.0v4 on OS X for script steps and

            data viewer evaluation.

             

            What version of FileMaker are you running?

             

            All the best,

             

             

            Tony White

            Tony White Designs, Inc.

            44 Butler Place, 6G

            Brooklyn, NY  11238

            Tel: 718-797-4175

            Tel: 718-398-2428

            tony_white@twdesigns.com

            http://www.twdesigns.com

            iChat: tonywhitelive@aim.com | skype: tonywhitelive

            • 3. Re: Variable Type Casts ?
              brsamuel

              Is Year a reserved word?  Try changing the variable name and see if your results change.

              • 4. Re: Variable Type Casts ?
                lhoong

                From FMP Help ( Home > Designing and creating databases > Creating a database > Using variables ):

                 

                "The data type of a variable is determined dynamically based on the assigned data. The default data type is text."

                 

                Lee.

                =================

                Binary Assist

                220 Gaines Oak Way

                Suwanee, GA 30024

                Voice: (678) 313-5604

                Internet: lhoong@binaryassist.com

                Website: www.binaryassist.com

                 

                FileMaker Certified Developer

                • 5. Re: Variable Type Casts ?
                  BruceHerbach

                  Hi,

                   

                  I haven't seen a set of rules for this, but have run into the issue.  In my case i was pulling the value from a field.  When I first wrote the script it seemed to work as expected.   Then got a complaint that the process failed.  Running it through the debugger showed the same issue.  The solution was the same. Getasnumber($varible).

                   

                  HTH

                  Bruce

                  • 6. Re: Variable Type Casts ?
                    beverly

                    Hey, Bruce! I tend to type cast, just because I know it will work. Do you have a problem with having to do that?

                     

                    Beverly

                    • 7. Re: Variable Type Casts ?
                      datastride

                      Having been stung by mismatched data types more than once (my bad, not a FileMaker problem), I now always type cast local and global variables. Not only does this ensure the comparisons will work as I am expecting, but it also provides just a bit of extra documentation … especially in those cases where a variable might hold text or a number or a date, depending on the data being processed at the time.

                       

                       

                       

                      Peace, love & brown rice,

                       

                      Morgan Jones

                       

                       

                       

                      FileMaker + Web:  Design, Develop & Deploy

                       

                      Certifications: FileMaker 9, 10 & 11

                       

                      <http://www.onepartharmony.com/> One Part Harmony 

                       

                      Austin, Texas • USA

                       

                      512-422-0611

                      • 8. Re: Variable Type Casts ?
                        gdurniak

                        We are spoiled rotten. This may be normal for programmers , but not for FileMaker Developers  :-)

                         

                        By the way, I can not so far reproduce this in a test file

                         

                        And the original script is too large, and convoluted

                         

                        And the step that failed was actually  IF $Year <= 0,  where $Year was set to 2012,  and did display as "2012" in the resulting Error Dialog

                         

                         

                        >Hey, Bruce! I tend to type cast, just because I know it will work. Do you have a problem with having to do that?

                         

                        Beverly

                        • 9. Re: Variable Type Casts ?
                          comment

                          Well, the thing is that all of the following are true::

                           

                          GetAsText ( 2012 ) > 0

                           

                          GetAsDate ( 2012 ) > 0

                           

                          GetAsTime ( 2012 ) > 0

                           

                          GetAsTimestamp ( 2012 ) > 0

                           

                           

                          So if this is a type issue, I wonder what type your variable can be in order to return false.

                          • 10. Re: Variable Type Casts ?
                            gdurniak

                            I did find one reproducible example:

                             

                            If $Year is set from a Text Field that contains  " 2102"  e.g.  preceded by a space

                             

                            $Year is <= 0   ( what probably happened to me )

                             

                            If $Year is set from a Text Field that contains  "2102"  e.g.  no space

                             

                            $Year is >  0

                             

                            If $Year is set from a Text Field that contains  " A"  e.g.  Letter only, preceded by a space

                             

                            $Year is <= 0  ( somewhat of a surprise )

                             

                            If $Year is set from a Text Field that contains  "A"  e.g.  Letter only, no space

                             

                            $Year is >  0

                             

                             

                            The same test using a number field is as expected. The space is ignored, and the A is <= 0

                             

                            My guess is I had an extra space somewhere

                             

                            greg

                            • 11. Re: Variable Type Casts ?
                              beverly

                              and that's why I type cast LOL! those carbon-based errors need a little help capturing.

                               

                              Beverly

                              • 12. Re: Variable Type Casts ?
                                datastride

                                But just remember that type casting variables is good practice:

                                 

                                ---------------------------------------------------------------------

                                 

                                Assume these assignments:

                                     $Year_1 = 935

                                     $Year_2 = 2012

                                 

                                So:

                                 

                                     $Year_2 >= $Year_1 will evaulate as "True"

                                 

                                And:

                                 

                                     GetAsNumber( $Year_2 ) >= GetAsNumber( $Year_1 ) will evaluate as "True".

                                 

                                -----------------------------------------------------------------

                                 

                                Now consider this example:

                                     $Year_1 = "935"

                                     $Year_2 = "2012"

                                 

                                So:

                                 

                                     $Year_2 >= $Year_1 will evaulate as "False"

                                 

                                But:

                                 

                                     GetAsNumber( $Year_2 ) >= GetAsNumber( $Year_1 ) will evaluate as "True".

                                 

                                So depending on the data type of the field or constant that was used to assign a value to a variable, the results could be different (and unexpected) without type casting.

                                • 13. Re: Variable Type Casts ?
                                  comment

                                  Morgan Jones wrote:

                                   

                                  So depending on the data type of the field or constant that was used to assign a value to a variable, the results could be different (and unexpected) without type casting.

                                   

                                  A small quibble: different - yes; unexpected - no.

                                  • 14. Re: Variable Type Casts ?
                                    HOnza

                                    Interesting discussion...

                                    Just for your info, if you want to easily and reliably check the value type stored in variable you can use 24U Toolbox Plug-In's function Toolbox_ValueType.

                                     

                                    HOnza

                                    1 2 Previous Next