3 Replies Latest reply on Apr 19, 2006 1:07 AM by YogeshM

    IIF function

    YogeshM
      Hi,

      I'd be grateful if somebody could please help me out with this problem.

      I actually want to write the following using the IIF statement:

      <cfif temp gt 0>
      <cfset label = "Over">
      <cfelseif temp lt 0>
      <cfset label = "Under">
      <cfelse>
      <cfset label = "-">
      </cfif>

      Note
      If I output the variable label using the cfif, cfelseif and cfelse, it gives me the correct output, i.e. either "-", Over, or Under.


      I tried the following to rewrite the above statement using the iif function:

      <cfset label = iif(temp eq 0,DE("-"),(iif(temp gt 0,DE("Over"),DE("Under"))))>

      Now, when temp eq 0, it works fine.
      However, when temp is lt 0, it throws me an error message saying variable Under is undefined.
      And when temp is gt 0, I get the same error message, but in this case it says variable Over is undefined.

      However, if I replace Over and Under by 1 or 0 (i.e. integers) it works perfect!

      Any idea how to get the output either Over or Under using the iif statement pls?

      Thanks and regards,
      Yogesh Mahadnac.
        • 1. Re: IIF function
          Level 7
          > I actually want to write the following using the IIF statement:

          Not that you asked, but IMO this is a mistake, for the very reason you're
          experiencing here: the iif() version of this code is quite difficult to
          read (and easy to muck up, as you have).

          WHY are you wanting to change it to an iif() expression?

          > <cfset label = iif(temp eq 0,DE("-"),(iif(temp gt 0,DE("Over"),DE("Under"))))>

          I *suspect* (strongly ~) this is because you are "escaping" the overs and
          unders in the nested iff() call, but not in the result of the outer one.
          So, if temp happens to be greater than zero, for all intents and purposes
          you are left with this line of code:

          iif(temp eq 0,DE("-"), "Over")

          If this is the case, I think you need to have a DE() around you entire
          second iif() expression.

          BLEAH.

          Stick with the CFML

          (just opinion).

          --
          Adam
          • 2. Re: IIF function
            TSB
            <cfset label = iif(temp eq 0,DE("-"),DE(iif(temp gt 0,DE("Over"),DE("Under"))))>

            You needed a DE before your second iif.

            Trevor
            Don't Click Here
            • 3. Re: IIF function
              YogeshM Level 1
              Thanks a lot TSB!

              That did the trick.

              Cheers!

              Yogesh