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. IIF strange behaviour

IIF strange behaviour

Scheduled Pinned Locked Moved Visual Basic
13 Posts 8 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.
  • D Offline
    D Offline
    ddecoy
    wrote on last edited by
    #1

    Dim o = IIf(True, "1", CType("", Decimal))

    This gives you an invalid cast exception. weak..

    D D D D G 5 Replies Last reply
    0
    • D ddecoy

      Dim o = IIf(True, "1", CType("", Decimal))

      This gives you an invalid cast exception. weak..

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      i always wanted to know that......................:~

      Dave Don't forget to rate messages!
      Find Me On: Web|Facebook|Twitter|LinkedIn
      Waving? dave.m.auld[at]googlewave.com

      L 1 Reply Last reply
      0
      • D DaveAuld

        i always wanted to know that......................:~

        Dave Don't forget to rate messages!
        Find Me On: Web|Facebook|Twitter|LinkedIn
        Waving? dave.m.auld[at]googlewave.com

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        I bet you still want to know why that happens? :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        D D 2 Replies Last reply
        0
        • L Luc Pattyn

          I bet you still want to know why that happens? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          D Offline
          D Offline
          Dalek Dave
          wrote on last edited by
          #4

          Tune in next week to see the next thrilling installment...

          ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave

          1 Reply Last reply
          0
          • L Luc Pattyn

            I bet you still want to know why that happens? :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            D Offline
            D Offline
            ddecoy
            wrote on last edited by
            #5

            You bet...

            1 Reply Last reply
            0
            • D ddecoy

              Dim o = IIf(True, "1", CType("", Decimal))

              This gives you an invalid cast exception. weak..

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Yeah, and ....?? It doesn't surprise me in the least. You cannot convert String.Emtpy to a Decimal type and, the way you coded it, IIF can either return a String ("1") or Decimal. The compiler can't figure out what the primary type of the return value is supposed to be and there is no implicit conversion available to go from String to Decimal. If you has IIF returning two different numeric types, say Integer and Short, this wouldn't be a problem because a Short can be upsized into an Integer, so the return type for IIF would be Integer.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              D 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Yeah, and ....?? It doesn't surprise me in the least. You cannot convert String.Emtpy to a Decimal type and, the way you coded it, IIF can either return a String ("1") or Decimal. The compiler can't figure out what the primary type of the return value is supposed to be and there is no implicit conversion available to go from String to Decimal. If you has IIF returning two different numeric types, say Integer and Short, this wouldn't be a problem because a Short can be upsized into an Integer, so the return type for IIF would be Integer.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008
                But no longer in 2009...

                D Offline
                D Offline
                ddecoy
                wrote on last edited by
                #7

                So why can I compile this then? In c# you can't...

                L L 2 Replies Last reply
                0
                • D ddecoy

                  So why can I compile this then? In c# you can't...

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  C# is a real programming language. VB/VB.NET isn't by default; you can improve things a bit by starting with OPTION STRICT ON. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  1 Reply Last reply
                  0
                  • D ddecoy

                    So why can I compile this then? In c# you can't...

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Does C# have a CType[^] function? I'd guess that C# checks your cast at compiletime, whereas the CType function may throw runtime errors. They're not the same languages, so behaviour doesn't have to be similar :)

                    I are Troll :suss:

                    1 Reply Last reply
                    0
                    • D ddecoy

                      Dim o = IIf(True, "1", CType("", Decimal))

                      This gives you an invalid cast exception. weak..

                      D Offline
                      D Offline
                      ddecoy
                      wrote on last edited by
                      #10

                      I thougth IIF was something special, like if the condition is true ,the false part is never reached. But it's nothing more than this:

                      Public Function IIF(ByVal condition As Boolean, ByVal truepart As Object, ByVal falsepart As Object) As Object
                      If condition Then
                      Return truepart
                      Else
                      Return falsepart
                      End If
                      End Function

                      D 1 Reply Last reply
                      0
                      • D ddecoy

                        I thougth IIF was something special, like if the condition is true ,the false part is never reached. But it's nothing more than this:

                        Public Function IIF(ByVal condition As Boolean, ByVal truepart As Object, ByVal falsepart As Object) As Object
                        If condition Then
                        Return truepart
                        Else
                        Return falsepart
                        End If
                        End Function

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #11

                        Yep. IIF is a function that returns a single type, just like any other. The reason why VB.NET let's you get away with some of this stuff at compile time is because, by default, VB will try to provide implicit conversions to what it thinks the expected types should be where as C# will not. Like Luc said, the way to avoid this is Option Strict On.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007, 2008
                        But no longer in 2009...

                        1 Reply Last reply
                        0
                        • D ddecoy

                          Dim o = IIf(True, "1", CType("", Decimal))

                          This gives you an invalid cast exception. weak..

                          D Offline
                          D Offline
                          Dave Doknjas
                          wrote on last edited by
                          #12

                          In VB9 (VS 2008) and beyond use the new VB ternary operator (which provides exactly the same behavior as the ternary conditional operator in C#, C++, and Java) rather than the 'IIf' function:

                          Dim o = If(True, "1", CType("", Decimal))

                          David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

                          1 Reply Last reply
                          0
                          • D ddecoy

                            Dim o = IIf(True, "1", CType("", Decimal))

                            This gives you an invalid cast exception. weak..

                            G Offline
                            G Offline
                            Gregory Gadow
                            wrote on last edited by
                            #13

                            An empty string cannot be converted to a Decimal so the conversion throws an error. Change the empty string to "0" and it should work just fine. If you need it to return a different type (this has the smell of a homework project) try replacing the CType block with a different value, like 0.0 or Nothing.

                            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