Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Lazy Evaluation ternary op?

Lazy Evaluation ternary op?

Scheduled Pinned Locked Moved Visual Basic
javaquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eidylon
    wrote on last edited by
    #1

    I know MS (FINALLY) added in lazy evaluation versions of And (AndAlso) and Or (OrElse). Have they perchance introduced a lazy eval version of the ternary-operator function (Iif)? Thats one thing i sorely miss from the C/Java world is the nice ?: operator. Iif can sort of emulate it, but Iif always evaluates both conditions (true AND false)... where as the ?: one only evaluates which ever one is selected by the condition. The Non-Lazy Iif is all but useless for the most very simplest simplest basic cases. Thanks in advance

    D 1 Reply Last reply
    0
    • E eidylon

      I know MS (FINALLY) added in lazy evaluation versions of And (AndAlso) and Or (OrElse). Have they perchance introduced a lazy eval version of the ternary-operator function (Iif)? Thats one thing i sorely miss from the C/Java world is the nice ?: operator. Iif can sort of emulate it, but Iif always evaluates both conditions (true AND false)... where as the ?: one only evaluates which ever one is selected by the condition. The Non-Lazy Iif is all but useless for the most very simplest simplest basic cases. Thanks in advance

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      Whosit wrote: Thats one thing i sorely miss from the C/Java world is the nice ?: operator. Iif can sort of emulate it, but Iif always evaluates both conditions (true AND false)... where as the ?: one only evaluates which ever one is selected by the condition. The Non-Lazy Iif is all but useless for the most very simplest simplest basic cases. I suspect Iif is implemented as a function. See, the worst thing about Iif is that it return an object, and if Option Strict is On, you'll need to cast, even if both types are correct.


      It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

      M 1 Reply Last reply
      0
      • D Daniel Turini

        Whosit wrote: Thats one thing i sorely miss from the C/Java world is the nice ?: operator. Iif can sort of emulate it, but Iif always evaluates both conditions (true AND false)... where as the ?: one only evaluates which ever one is selected by the condition. The Non-Lazy Iif is all but useless for the most very simplest simplest basic cases. I suspect Iif is implemented as a function. See, the worst thing about Iif is that it return an object, and if Option Strict is On, you'll need to cast, even if both types are correct.


        It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

        M Offline
        M Offline
        mikasa
        wrote on last edited by
        #3

        Ha! That's not the worst thing about "IIF". The worst thing is that it tries to Evaluate both Expressions before returning a Value. What it should do is only Evaluate the Expression and then Return the Value that is absolutely neccessary! For example, you can never do this: Dim MyObject As Object = Nothing rsData.Fields("MyField").Value = IIF(IsNothing(MyObject), "", MyObject.Name) This will always give you an Error because MyObject is nothing when it tries to Evaluate "MyObject.Name". So I ask...why does it do that? :wtf:

        D 1 Reply Last reply
        0
        • M mikasa

          Ha! That's not the worst thing about "IIF". The worst thing is that it tries to Evaluate both Expressions before returning a Value. What it should do is only Evaluate the Expression and then Return the Value that is absolutely neccessary! For example, you can never do this: Dim MyObject As Object = Nothing rsData.Fields("MyField").Value = IIF(IsNothing(MyObject), "", MyObject.Name) This will always give you an Error because MyObject is nothing when it tries to Evaluate "MyObject.Name". So I ask...why does it do that? :wtf:

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          Because IIf is not an operator, it's a function, defined like this:

          Function IIf(Byval x as boolean, Byval o1 as object, Byval o2 as object) as Object
          If x then
          Return o1
          Else
          Return o2
          End if
          End Function

          See?


          It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

          M 1 Reply Last reply
          0
          • D Daniel Turini

            Because IIf is not an operator, it's a function, defined like this:

            Function IIf(Byval x as boolean, Byval o1 as object, Byval o2 as object) as Object
            If x then
            Return o1
            Else
            Return o2
            End if
            End Function

            See?


            It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

            M Offline
            M Offline
            mikasa
            wrote on last edited by
            #5

            Yes, True! I wonder if there is a better way to accomplish what I was explaining previously??

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups