IIF strange behaviour
-
Dim o = IIf(True, "1", CType("", Decimal))
This gives you an invalid cast exception. weak..
-
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.
-
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.
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
-
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.
-
Dim o = IIf(True, "1", CType("", Decimal))
This gives you an invalid cast exception. weak..
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... -
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... -
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.
-
Dim o = IIf(True, "1", CType("", Decimal))
This gives you an invalid cast exception. weak..
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 -
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 FunctionYep. 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... -
Dim o = IIf(True, "1", CType("", Decimal))
This gives you an invalid cast exception. weak..
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
-
Dim o = IIf(True, "1", CType("", Decimal))
This gives you an invalid cast exception. weak..
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 theCType
block with a different value, like0.0
orNothing
.