Udostępnij za pośrednictwem


Cannot infer a common type for the first and second operands of the binary 'If' operator

Cannot infer a common type for the first and second operands of the binary 'If' operator. One must have a widening conversion to the other.

The binary If operator requires that there be a widening conversion between one of the arguments and the other argument. For example, because there is not a widening conversion in either direction between Integer and String, the following code causes this error.

Dim first? As Integer
Dim second As String = "First is Nothing"
'' Not valid.
' Console.WriteLine(If(first, second))

Error ID: BC33110

To correct this error

  • Provide an explicit conversion for one of the operands, if that is possible in your code:

    Console.WriteLine(If(first, CInt(second))) 
    
  • Rewrite the code by using a different conditional construction.

    If first IsNot Nothing Then
        Console.WriteLine(first)
    Else
        Console.WriteLine(second)
    End If
    

See Also

Concepts

Widening and Narrowing Conversions

Reference

If Operator

If...Then...Else Statement (Visual Basic)