With regard to casting the If result, I notice that if VS cannot infer a common type for the 2nd and 3rd arguments, I get a blue squiggly error:
Error 1 Cannot infer a common type for the second and third operands of the 'If' operator. One must have a widening conversion to the other.
Hence it seems the compiler must know the return type and could arrange to cast the result automatically. In fact, take a look at this:
And sure enough I get "Type of o is System.Double"
So maybe explicit casting is unnecessary?
Error 1 Cannot infer a common type for the second and third operands of the 'If' operator. One must have a widening conversion to the other.
Hence it seems the compiler must know the return type and could arrange to cast the result automatically. In fact, take a look at this:
Dim d1 As Double |
Dim d2 As Double |
d1 = 1.0R |
d2 = 2.0R |
Dim o As Object |
o = If(True, d1, d2) |
Debug.Print("Type of o is {0}", o.GetType.ToString) |
And sure enough I get "Type of o is System.Double"
So maybe explicit casting is unnecessary?